90

this is my checkbox

HTML

<label class="checkbox">
    <input id="eu_want_team" name="eu_want_team" type="checkbox">
</label>

JQuery

var eu_want_team = $('#eu_want_team').val();
alert(eu_want_team);

Its always displaying ON, is it checked or not. Whats the problem with it?

Coder
  • 6,948
  • 13
  • 56
  • 86
  • 1
    Welcome to Stack Overflow. I've re-tagged as `jQuery`, assuming that's where `$()` and `val()` come from. Feel free to correct it if I was wrong. And next time you ask about a non-native JavaScript function don't forget to post its code or mention what library it belongs to. – Álvaro González Aug 22 '13 at 10:08
  • 1
    u should use `.prop('checked')` in jQuery – TechDogLover OR kiaNasirzadeh Apr 08 '18 at 12:12

6 Answers6

70

Use .is(':checked') instead: Working jsFiddle

var eu_want_team = $('#eu_want_team').is(':checked');
alert(eu_want_team);

or as @Itay said in comments you can use jQuery's .prop() to get the checked property value:

alert($("#eu_want_team").prop("checked"));
Yotam Omer
  • 15,310
  • 11
  • 62
  • 65
14
<label class="checkbox">
    <input id="eu_want_team" name="eu_want_team" type="checkbox" value="somevalue">
</label>

<script>
   var ele = document.getElementById("eu_want_team");
   if(ele.checked)
   alert(ele.value)

</script>
Voonic
  • 4,667
  • 3
  • 27
  • 58
2

This will work :

if ($('#element').is(":checked")) {
    eu_want_team = 1;
} else {
    eu_want_team = 0;
}
alert(eu_want_team);
Roy M J
  • 6,926
  • 7
  • 51
  • 78
1

Have a quick look at this answer for checking if a checkbox is checked.

How to check whether a checkbox is checked in jQuery?

But basically you want to do something like below to check its value:

if ($("#element").is(":checked")) {
  alert("I'm checked");
}
Community
  • 1
  • 1
Mark
  • 2,184
  • 1
  • 16
  • 28
1

i think this is what you want to do

$("#eu_want_team").click(function(){
    alert($(this).is(':checked')); 
}
Kimtho6
  • 6,154
  • 9
  • 40
  • 56
0

Try this

if ( $('#element').is(':checked')){
    alert(element);
   }
Padmanathan J
  • 4,614
  • 5
  • 37
  • 75