2

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

3 Answers 3

2

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>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer, but I tryed something different now which worked.
0

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.

1 Comment

Thank you, i came to the same result ;)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.