Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checkbox jquery or javascript oncheck?

I do not know the correct terminology for this, but I want the same effect as onclick but for a check box with jquery or javascript.

onclick version:

<a href="..." onclick="function()">Link</a>

I want the same effect as above but for a checkbox. The end result will be that the page should reload with an updated php query, but that part I can do. I just don't know what the onclick is for checkboxes.

checkbox:

<input type="checkbox" name="change" value="one" />changes php query
like image 290
cbr0wn Avatar asked Aug 29 '11 21:08

cbr0wn


People also ask

How do you check checkbox is checked or not JavaScript?

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.

Is checkbox checked or not jQuery?

To check whether a Checkbox has been checked, in jQuery, you can simply select the element, get its underlying object, instead of the jQuery object ( [0] ) and use the built-in checked property: let isChecked = $('#takenBefore')[0]. checked console. log(isChecked);

How can create checkbox click event in jQuery?

change() updates the textbox value with the checkbox status. I use . click() to confirm the action on uncheck. If the user selects cancel, the checkmark is restored but .


2 Answers

You should listen to the change event, as the checkbox can be selected or deselect with the keyboard too:

$('input[type="checkbox"][name="change"]').change(function() {
     if(this.checked) {
         // do something when checked
     }
 });

Similarly with plain JavaScript:

// checkbox is a reference to the element

checkbox.onchange = function() {
     if(this.checked) {
         // do something when checked
     }
};

And last, although you really should not use inline event handlers, but if you have to:

<input ... onchange="handler.call(this)" />

where handler is like the handlers shown above.


Further reading:

  • jQuery documentation
  • MDN JavaScript Guide
  • quriksmode.org Introduction to Events
like image 66
Felix Kling Avatar answered Sep 25 '22 14:09

Felix Kling


$('#input_id').click(function() {
    // do what you want here...
});
like image 29
Burak Erdem Avatar answered Sep 25 '22 14:09

Burak Erdem