1

How can i change this:

function getCheckedRadioValue(name) { var elements = document.getElementsByName(name); for (var i=0, len=elements.length; i<len; ++i) if (elements[i].checked) return elements[i].value; } 

into jQuery? is it posible to make it simpler?

0

2 Answers 2

0

By using:

function getCheckedRadioValue(name) { return $('input[name='+name+']:radio:checked').val(); } 
Sign up to request clarification or add additional context in comments.

Comments

0

You can do

$('input[type=radio]').each(function(){ if($(this).is(':checked')){ alert('this button is checked'); } }); 

See the working example below:

$('button').click(function() { var counter = 1; $('input[type=radio]').each(function() { if ($(this).is(':checked')) { alert(' button ' + (counter) + ' is checked'); } counter++; }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type=radio checked /> <input type=radio /> <input type=radio checked /> <button>check</button>

View on JSFiddle

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.