In my html:
<input type="checkbox" name="select"> <input type="radio" name="select">
name property same due to some logical reason
in my JS
I write below code:
$('input[type="checkbox"],input[type="radio"]').on("change",function(event){ console.log(event.target.nodename); // on both cases it show "INPUT" } });
How I will know that I click on checkbox or radio button?
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.
To find the selected radio button, you follow these steps: Select all radio buttons by using a DOM method such as querySelectorAll() method. Get the checked property of the radio button. If the checked property is true , the radio button is checked; otherwise, it is unchecked.
To check the element type pf an event target, using the is() method.
We can check the status of a radio button by using the :checked jQuery selector together with the jQuery function is . For example: $('#el').is(':checked') . It is exactly the same method we use to check when a checkbox is checked using jQuery.
.nodeName
gives the html
tag used so you have to use .type
to get the type of the node there.
Try this one:
console.log(event.target.type);
Demo
For those of you looking for the pure JavaScript way, it's event.target.tagName
.
That property is an uppercase string of the element type, like "A", "UL", "LI", etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With