Link to home
Start Free TrialLog in
Avatar of JaXL
JaXL

asked on

How do you check if a radio button has the attribute 'checked' with Prototype

Hello,

I have a bit of code that checks a list if LI tags which contain radio button input's.  I have some clever logic via the Chocolate Chip Javascript framework library to work out when an LI is clicked, it will apply a relevant class to display the radio button has been selected.

However, I want to expand that logic so that it digs deeper into the LI and finds which radio button input is the one that is already selected (prior to any user choosing anything) when the page loads and apply a class to it so that it instantly highlights what is already selected.

I'm a bit new to Prototype so I'm not sure what is the best approach to do this so would appreciate any help you can offer.

So in the case below, I want to pick out button 3.

JSFiddle Link: http://jsfiddle.net/Qw6KA/

HTML:
<ul class="radioList">
   <li>
      <input type="radio" id="radio1" name="radioButton" value="Button 1">
      <label for="radio1">Button 1</label>
   </li>
   <li>
      <input type="radio" id="radio2" name="radioButton" value="Button 2">
      <label for="radio2">Button 2</label>
   </li>
   <li>
      <input type="radio" id="radio3" name="radioButton" value="Button 3" checked="checked">
      <label for="radio3">Button 3</label>
   </li>
   <li>
      <input type="radio" id="radio4" name="radioButton" value="Button 4">
      <label for="radio4">Button 4</label>
   </li>
</ul>

Open in new window



JS (Prototype):
$.RadioButtons = function( viewSelector, callback ) {
		var items = viewSelector + ".radioList li";
		var radioButtons = $$(items);
		radioButtons.forEach(function(item) {
			item.bind("click", function() {
				radioButtons.forEach(function(check) {
					check.removeClass("selected");
				});
				this.addClass("selected");
				this.last().checked = true; 
				if (callback) {
					callback(item);
				}
			});
		});
	};

Open in new window


Thanks
-JaXL
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Check this example : http://jsfiddle.net/KMcbG/
$.RadioButtons = function( viewSelector, callback ) {
    var items = viewSelector + ".radioList li";
    var radioButtons = $$(items);
    radioButtons.forEach(function(item) {
        Event.observe(item, 'click', function() {
            radioButtons.forEach(function(check) {
                check.removeClassName("selected");
            });
            this.addClassName("selected");
            this.firstDescendant().checked = true; // but why?
            if (callback) {
                callback(item);
            }
        });
    });
};

$.RadioButtons("ul",ok);

function ok(a,b,c,d,e) {
    alert("ok");
}

Open in new window

Avatar of JaXL
JaXL

ASKER

Thanks leakim971.

Is it possible to isolate the selected radio button without clicking, say on page load?
<< isolate >>?
Avatar of JaXL

ASKER

Sorry - my last comment probably made no sense at all.

When the page loads, I want to highlight the radio button that is already checked straight away.  Although I do wish to keep the click function so when a user chooses another radio button it will remove the highlight and highlight the new choice.

Hopefully that makes more sense
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial