2

Imagine I have an input like so:

<input id="submitCheckbox" type="checkbox" name="recieveEmails"> 

And some form of submit button, say;

<button id="submit" type="button" onclick="submit()"></button> 

And Javascript like so:

function submit() { var newVar = document.getElementById("submitCheckbox"); } 

And by using something like this, newVar would be equal to True or False depending on the status of the checkbox when the submit button is pressed?

PS. The Javascript example I provided is theoretical. Please don't rip into me because it "doesn't work"

4
  • 1
    You'd have to get the state with document.getElementById("submitCheckbox").checked Commented Feb 23, 2017 at 6:34
  • var newVar = document.getElementById("submitCheckbox").checked; Commented Feb 23, 2017 at 6:34
  • This would provide a boolean result? @adeneo Commented Feb 23, 2017 at 6:34
  • Thanks all. .Checked works great Commented Feb 23, 2017 at 6:38

1 Answer 1

1

Use this document.getElementById("submitCheckbox").checked instead of document.getElementById("submitCheckbox") as shown here:

function submit() { var newVar = document.getElementById("submitCheckbox").checked; console.log(newVar); }
<input id="submitCheckbox" type="checkbox" name="recieveEmails"> <br> <button id="submit" type="button" onclick="submit()">Click</button>

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

2 Comments

Thanks Manish. I'll accept the answer as soon as I can
welcome Caspar!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.