Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

By event.target, how I know that it is checkbox or it is radio?

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?

like image 878
Rituraj ratan Avatar asked Nov 15 '13 06:11

Rituraj ratan


People also ask

What event is used 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 check whether radio button is checked or not?

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.

How do you determine the target element of an event?

To check the element type pf an event target, using the is() method.

How do you check whether a radio button is checked or not in jQuery?

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.


2 Answers

.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

like image 117
Jai Avatar answered Sep 17 '22 18:09

Jai


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.

like image 27
Jamey Avatar answered Sep 20 '22 18:09

Jamey