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
JaXLAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
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

leakim971PluritechnicianCommented:
JaXLAuthor Commented:
Thanks leakim971.

Is it possible to isolate the selected radio button without clicking, say on page load?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

leakim971PluritechnicianCommented:
<< isolate >>?
JaXLAuthor Commented:
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
leakim971PluritechnicianCommented:
var el = $$("ul.radioList input:checked")[0];
el.parentNode.addClassName("selected");

test page : http://jsfiddle.net/KMcbG/2/

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.