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();
}
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.