14

Hi this is a jquery question:

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:

$("#submit").click(function(){

          if($(".select").is(':checked')){

              alert($(".select").attr('id'));
          }
});

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!

Aruna
  • 11,959
  • 3
  • 28
  • 42
muffin
  • 2,034
  • 10
  • 43
  • 79

8 Answers8

45

Are you only checking one checkbox at a time?

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

Or, if you have multiple checkboxes checked at once:

$(".select:checked").each(function(){
   alert($(this).attr('id'));
});

Demo: http://jsfiddle.net/UTux2/

XCS
  • 27,244
  • 26
  • 101
  • 151
3

Get ID using this way

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

Also keep in mind that if only one can be selected, it's better to change it to radio.

Elon Than
  • 9,603
  • 4
  • 27
  • 37
2

You aren't capturing the checked checkbox, you're only asking "is there one checked?".

$("#submit").click(function(){

    var elem = $(".select:checked");

    if(elem.length > 0){
        alert(elem.attr('id'));
    }
});
MrCode
  • 63,975
  • 10
  • 90
  • 112
2
$("#submit").click(function(){  
          if($(".select").is(':checked')){
               var $this=$(".select").is(':checked');
               alert($this.attr('id'));
          }
});
Somnath Kharat
  • 3,570
  • 2
  • 27
  • 51
2

There are multiple elements. You need to check for all checkbox having same class

$("#submit").click(function(){

          $(".select:checked").each(function(){
           alert($(this).attr('id'));
   });

});
sudhansu63
  • 6,025
  • 4
  • 39
  • 52
2

you should be using radio buttons and not checkboxes to allow one choice out of many.

<input type="radio" name="select" value="select-1">
<input type="radio" name="select" value="select-2">

then things will get really simple:

$("#submit").click(function(){

    alert($('[name="select"]:checked').val());
});
Rafael Herscovici
  • 16,558
  • 19
  • 65
  • 93
0

try something like this, you don't need to iterate

        $(document).ready(function () {
            $("#submit").click(function(){
                  if($(".select").is(':checked')){
                      alert($(".select:checked").attr('id'));
                  }
            });
        });
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40
-1

Because you have not iterated all the elements which has class 'select'. It always goes to the first element and prints the result. Put a loop to iterate all elements having class 'select'.

swapnil
  • 1
  • 3