Tag Archives: JQuery checkbox

JQuery: How to get the selected values of checkbox, radio and select

JQuery gets the selected value of radio:

<input type="radio" name="rd" id="rd1" value="1">1
<input type="radio" name="rd" id="rd2" value="2">2
<input type="radio" name="rd" id="rd3" value="3">3

All three methods are OK

$('input:radio:checked').val();
$("input[type='radio']:checked").val();
$("input[name='rd']:checked").val();

JQuery gets the selected value of select

  <select name="products" id="sel">
    <option value='1'>option1</option>
    <option value='2' selected>option2</option>
    <option value='3'>option3</option>
    <option value='4'>option4</option>
  </select>

Get the value of the selected item:

$('select#sel option:selected').val();
or
$('select#sel').find('option:selected').val();

Get the text value of the selected item:

$('select#seloption:selected').text();
or
$('select#sel').find('option:selected').text();

Get the index value of the currently selected item:

$('select#sel').get(0).selectedIndex;

JQuery gets the selected value of the check box:

<input type="checkbox" name="ck" value="checkbox1">checkbox1     
<input type="checkbox" name="ck" value="checkbox2" checked>checkbox2     
<input type="checkbox" name="ck" value="checkbox3">checkbox3      

Get a single check box selected item:

$("input:checkbox:checked").val() 
or 
$("input:[type='checkbox']:checked").val(); 
or 
$("input:[name='ck']:checked").val(); 

Get multiple check box items:

$('input:checkbox').each(function() { 
    if ($(this).attr('checked') ==true) { 
        alert($(this).val()); 
    } 
});