By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Deselect checkbox if confirm is false

KeredDrahcir
100+
P: 426
I have a series of checkboxes that uses can click on which will cause a form to submit. Before I submit the form I wasn't them to confirm that's what they want to do.
I can't seem to uncheck the box if they choose cancel and I'm not sure what is wrong with the code I'm using. Can anyone help?

Expand|Select|Wrap|Line Numbers
  1. <input type="checkbox" name="box_1" id="box_1" value="1" onclick="javascript:if (confirm('Are you sure?')){this.form.submit();}else{document.getElementById('box_1').checked = 'false';};" />
Am I missing something obvious?
Feb 19 '15 #1

✓ answered by gits

since you are setting a javascript property of the dom-element you should set it as a boolean value and not as a string. basically it can be simplified as well like this:

Expand|Select|Wrap|Line Numbers
  1. <input type="checkbox" name="box_1" id="box_1" value="1" onclick="
  2.     if (confirm('Are you sure?')) {
  3.         this.form.submit();
  4.     } else {
  5.         this.checked = false;
  6.     };
  7. "/>
  8.  

Share this Question
Share on Google+
2 Replies


gits
Expert Mod 5K+
P: 5,270
since you are setting a javascript property of the dom-element you should set it as a boolean value and not as a string. basically it can be simplified as well like this:

Expand|Select|Wrap|Line Numbers
  1. <input type="checkbox" name="box_1" id="box_1" value="1" onclick="
  2.     if (confirm('Are you sure?')) {
  3.         this.form.submit();
  4.     } else {
  5.         this.checked = false;
  6.     };
  7. "/>
  8.  
Feb 20 '15 #2

KeredDrahcir
100+
P: 426
That makes sense. Thanks. I'll give it a try and let you know. The reason I was setting it as a string was becuase it hadn't worked so I'd tried something else but I have a feeling it's going to work. Thanks.
Feb 20 '15 #3

Post your reply

Sign in to post your reply or Sign up for a free account.