skip to Main Content

I have a boolean value called isPublic and i wanna set it to true or false depending on if the user checked the check box or not, how can i do that?

Here is the parameter :

isPublic : boolean = false;

And here is the checkbox :

<input type="checkbox" name="newGalleryIsPublic">Publique

So if the check box is checked, I wanna set the isPublic to true and if not, make it false.

Haven’t really found anything on that so if some can help it would be great!

2

Answers


  1. something like that ?

    const chkBox = document.querySelector('[name="newGalleryIsPublic"]');
    
    var isPublic = chkBox.checked;
     
    
    chkBox.addEventListener('change', evt =>
      {
      isPublic = chkBox.checked;
      console.clear();
      console.log(isPublic);
      });
    <label>
      <input type="checkbox" name="newGalleryIsPublic"> Publique
    </label>
    Login or Signup to reply.
  2. var checkbox = document.querySelector("input");
    var isChecked;
    checkbox.addEventListener('change', function() {
      if (this.checked) {
         isChecked = true
        
      } else {
        isChecked = false
      }
      console.log(isChecked)
    });
    <input type="checkbox" name="newGalleryIsPublic">Publique
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search