Reputation: 59
I have a checkbox on my site looking like this:
<input type="checkbox" class="form-control" id="jobfixedstart_cb" onclick="jobfixedstartfunc();" />
In a Javascript .onClick-Event I get the value of the checkbox like this:
var addjobfixedstart = document.forms["add-new-job"].jobfixedstart_cb.value;
Now, no matter if the checkbox is checked or not, the value of "addjobfixedstart" is always "on". If I give the checkbox a value, it always sends the value.
What am i doing wrong?
Edit: I checked the status of the variable with an "alert" after the variable like that:
alert(addjobfixedstart);
Upvotes: 2
Views: 809
Reputation: 59
I made it work by checking the checkbox like this:
var addjobfixedstart = document.getElementById("jobfixedstart_cb").checked
You get back a "true" of "false" value which you can work with.
Upvotes: 0
Reputation: 1322
You can try the following if you are trying to know whether a checkbox is checked or not
document.getElementById('jobfixedstart_cb').checked
This returns boolean. I hope this helps.
Upvotes: 0
Reputation: 193271
If you want to check checked status you should read checked
property. For checkbox value
read value
property. In addition, listen to onchange
event, rather then onclick
.
All together it will become:
function jobfixedstartfunc() {
var checkbox = document.forms["add-new-job"].jobfixedstart_cb;
alert('Checked:' + checkbox.checked + ', value:' + checkbox.value)
}
<form name="add-new-job">
<input type="checkbox" class="form-control" id="jobfixedstart_cb" onchange="jobfixedstartfunc();" />
</form>
Upvotes: 2