Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox value is always 'on' [duplicate]

People also ask

Does checkbox have value attribute?

The value attribute on the checkbox is used when the form is interacting with a server. So when you set the value as lettuce, that means when the form is submitted and that box is checked, the value it sends back to the server is topping=lettuce .

Can a checkbox have multiple values?

Where multiple controls exist, radio buttons allow one to be selected out of them all, whereas checkboxes allow multiple values to be selected. A string representing the value of the checkbox.

How do I make a checkbox always checked in HTML?

The checked attribute is a boolean attribute. When present, it specifies that an <input> element should be pre-selected (checked) when the page loads. The checked attribute can be used with <input type="checkbox"> and <input type="radio"> . The checked attribute can also be set after the page load, with a JavaScript.

Can multiple checkboxes have same name?

Groups of Checkboxes with the Same NameWhen grouping checkboxes and giving them each the same name, attach square brackets to the end of the name in order for server-side code to treat checked checkboxes as an array. Otherwise only the last checked item in the group would be available upon submission.


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"));

<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>

This will work :

if ($('#element').is(":checked")) {
    eu_want_team = 1;
} else {
    eu_want_team = 0;
}
alert(eu_want_team);