Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform an action on checkbox checked or unchecked event on html form

I have a checkbox on a form which is unchecked by default as usual. Now I want to perform two separated actions on the checked and unchecked state of this checkbox.

This is my checkbox:

<form>     syn<input type="checkbox" name="checkfield" id="g01-01"  onchange="doalert(this.id)"/> </form> 

And this is my script:

function doalert(id){   if(this.checked) {      alert('checked');   }else{      alert('unchecked');   } } 

It just alerts unchecked! What is the best way to do this?

like image 401
shekoufeh Avatar asked Sep 07 '15 11:09

shekoufeh


People also ask

How do I check if a checkbox is checked in an event?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.

How do you make a checkbox unchecked in HTML?

attr("checked","checked"); To uncheck the checkbox: $("#checkboxid").

What is the event for checkbox?

Checkbox event handling using pure Javascript There are two events that you can attach to the checkbox to be fired once the checkbox value has been changed they are the onclick and the onchange events.

How do you handle a checkbox in HTML?

The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!


2 Answers

We can do this using JavaScript, no need of jQuery. Just pass the changed element and let JavaScript handle it.

HTML

<form id="myform">     syn<input type="checkbox" name="checkfield" id="g01-01"  onchange="doalert(this)"/> </form> 

JS

function doalert(checkboxElem) {   if (checkboxElem.checked) {     alert ("hi");   } else {     alert ("bye");   } } 

Demo Here

like image 131
Shrinivas Pai Avatar answered Sep 29 '22 08:09

Shrinivas Pai


The problem is how you've attached the listener:

<input type="checkbox" ...  onchange="doalert(this.id)"> 

Inline listeners are effectively wrapped in a function which is called with the element as this. That function then calls the doalert function, but doesn't set its this so it will default to the global object (window in a browser).

Since the window object doesn't have a checked property, this.checked always resolves to false.

If you want this within doalert to be the element, attach the listener using addEventListener:

window.onload = function() {   var input = document.querySelector('#g01-01');   if (input) {        input.addEventListener('change', doalert, false);   } } 

Or if you wish to use an inline listener:

<input type="checkbox" ...  onchange="doalert.call(this, this.id)"> 
like image 25
RobG Avatar answered Sep 29 '22 06:09

RobG