Can this be made to work?

Joined
Jan 6, 2022
Messages
18
Reaction score
0
I know it doesn't make sense.
What I thought would happen is that when the check box is clicked the toggle function simply unchecks it.
Yes, I know , it doesn't make much sense out of context

Code:
<script>
function toggle() {
this.checkbox.checked = "false"
}
</script>
<p>status <input type="checkbox"  onchange="toggle()" >
 
Joined
Jul 4, 2023
Messages
466
Reaction score
57
Your code is not correct enough.
Check this:
HTML:
<script>
  function toggle(element) {
    element.checked = false;
  }
</script>
<p>status <input type="checkbox" onchange="toggle(this)"></p>
HTML:
<script>
  function toggle(e) {
    e.target.checked = false;
  }
</script>
<p>status <input type="checkbox" onchange="toggle(event)"></p>

BTW,
HTML:
<script>
  function toggle(element) {
    element.checked = !element.checked;
  }
</script>
<p>status <input type="checkbox" onchange="toggle(this)"></p>

// or

<script>
  function toggle(element) {
    element.checked = !!element.checked;
  }
</script>
<p>status <input type="checkbox" onchange="toggle(this)"></p>
HTML:
<script>
  function toggle(element) {
    console.log("Checkbox is now: " + (element.checked ? 'checked' : 'unchecked'));
  }
</script>
<p>status <input type="checkbox" onchange="toggle(this)"></p>
 
Last edited:
Joined
Jul 4, 2023
Messages
466
Reaction score
57
One more example.
HTML:
<p>status <input type="checkbox" name="Lorem"></p>
<script>
  const checkbox = document.querySelector('p input[type="checkbox"]');
 
  checkbox.addEventListener('change', function() {
    console.log("Checkbox is now: " + (this.checked ? 'checked' : 'unchecked'));
    console.log(this, typeof(this));
  });
</script>
 
Joined
Jul 4, 2023
Messages
466
Reaction score
57
BTW,
HTML:
<p>
  <label>status <input type="checkbox" name="Lorem"></label>   
</p>

<script>
  const checkbox = document.querySelector('p input');

  checkbox.addEventListener('change', function(e) {
    console.clear();
    console.warn('Checkbox is now: ' + (this.checked ? 'checked' : 'unchecked'));
    console.log('this:', this);
    console.log('this.toString():', this.toString(), typeof(this));
    console.log('typeof(this):', typeof(this));

    console.log('');

    console.info('e.target:', e.target);
    console.info('e.target.toString():', e.target.toString());
    console.info('typeof(e.target):', typeof(e.target));

    console.log('this.type:', this.type);
    console.log('this.name:', this.name);
    console.log('this.parentNode:', this.parentNode);
    console.log('this.parentNode.textContent:', this.parentNode.textContent);
    console.log('this.parentNode.innerHTML:', this.parentNode.innerHTML);

    console.info('this.parentNode.parentNode:', this.parentNode.parentNode);
    console.info("this.closest('p'):", this.closest('p'));
  });
</script>
 
Last edited:
Joined
Jan 6, 2022
Messages
18
Reaction score
0
BTW,
HTML:
<p>
  <label>status <input type="checkbox" name="Lorem"></label>  
</p>

<script>
  const checkbox = document.querySelector('p input');

  checkbox.addEventListener('change', function(e) {
    console.clear();
    console.warn('Checkbox is now: ' + (this.checked ? 'checked' : 'unchecked'));
    console.log('this:', this);
    console.log('this.toString():', this.toString(), typeof(this));
    console.log('typeof(this):', typeof(this));

    console.log('');

    console.info('e.target:', e.target);
    console.info('e.target.toString():', e.target.toString());
    console.info('typeof(e.target):', typeof(e.target));

    console.log('this.type:', this.type);
    console.log('this.name:', this.name);
    console.log('this.parentNode:', this.parentNode);
    console.log('this.parentNode.textContent:', this.parentNode.textContent);
    console.log('this.parentNode.innerHTML:', this.parentNode.innerHTML);

    console.info('this.parentNode.parentNode:', this.parentNode.parentNode);
    console.info("this.closest('p'):", this.closest('p'));
  });
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,930
Messages
2,570,072
Members
46,522
Latest member
Mad-Ram

Latest Threads

Top