1

If I have a checkbox and decide I need for it to throw a boolean when it is clicked, how would I do that? Would I use the onclick method or would I use data-js to change the var? I'm new to programming and am confused.

Checkbox:

<input id="private-player" type="checkbox" style="margin-right: 5px;" />
Graham
  • 7,431
  • 18
  • 59
  • 84
Sam F
  • 29
  • 1
  • 8
  • are you throwing a boolean to a server via standard form post/get? if so, that would probably just consist in a standard form where the form is submitted by the user after a selection is made. You likely want to give the input a name attribute, so the server can retrieve the GET or POST parameter and the associated value. You may also be able to accomplish that with some javaScript, but, if you're new to programming, probably best to fiddle around with a standard form post first. – Nik B Mar 03 '19 at 03:25
  • @NikB I am trying to just throw a window.alert when they click on the checkbox. I'm not trying to do anything with the server. – Sam F Mar 03 '19 at 03:28
  • i see. are you utilizing any javaScript? could attach an event listener on the input so that when an event registers, you can show an alert. – Nik B Mar 03 '19 at 03:29
  • 1
    this might help you out: https://stackoverflow.com/a/14544545/7367174 – Nik B Mar 03 '19 at 03:30
  • alright that helped a lot. Thanks @NikB – Sam F Mar 03 '19 at 04:31

1 Answers1

2

Call

function testCheckbox(cb) {
    // cb.checked is Boolean
    if (cb.checked) alert("checkbox is checked");
    else alert("checkbox is NOT checked");
};

From

<label>click to check/uncheck: </label><input 
   type="checkbox" onchange="testCheckbox(this);" /> 
Richard Uie
  • 141
  • 4