muffin muffin - 1 year ago 82
Javascript Question

Get the ID by Class name JQuery

Hi this is a


supposed i have this:

<input type="checkbox" class="select" id ="select-1">

<input type="checkbox" class="select" id ="select-2">

and i want to get the id, of the checkbox that has been selected when i click a submit button (note: you can only select 1)
and i have this button click event here:




It always alerts "select-1" even if i select the checkbox with the "select-2" id.. I'm guessing since they're both in the same class the first one with the ".select" class to be found is displayed in the alert.. how do i get the specific id that is checked by using their class names? Thank You!

Answer Source

Are you only checking one checkbox at a time?

alert( $(".select:checked").attr('id') );

Or, if you have multiple checkboxes checked at once:



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download