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

9

75

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?

Purge answered 15/11, 2013 at 6:0 Comment(1)
Instead of using JQuery you can (ES6+) also use Element.matches() which returns a boolean.Lui
D
89

.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

Diffuse answered 15/11, 2013 at 6:10 Comment(0)
E
81

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.

Essene answered 12/10, 2017 at 18:7 Comment(1)
This will always be INPUT since both radio buttons and checkboxes use the INPUT tag. You want to check the type property which will be either "radio" or "checkbox"Denitadenitrate
Z
10
console.log($(event.target).is(':radio'));
console.log($(event.target).is('[type="radio"]'));
Zetes answered 15/11, 2013 at 6:2 Comment(0)
R
0

you can use event.target.getAttribute('type') instead

Rematch answered 15/11, 2013 at 6:2 Comment(0)
I
0

Try to use attr() like,

console.log($(this).attr('type'));// checkbox or radio

Code

$('input[type="checkbox"],input[type="radio"]').on("change", function (event) {
    console.log($(this).prop('tagName'));// INPUT
    console.log($(this).attr('type'));// checkbox or radio
});
Internalcombustion answered 15/11, 2013 at 6:4 Comment(0)
B
0

For the node type with jQuery you can use:

  • $(event.target).attr('type')
  • event.target .type
$("SELECTOR").click(function (event) {
var idEventTarget = $(event.target).attr('id');
console.log($(event.target).attr('type'));
console.log(event.target.type);
});
Barri answered 24/12, 2018 at 11:13 Comment(0)
H
0

If the event passed to the progress or error event handler of an XMLHttpRequest, or an IDB success or error event handler, the target will be an XMLHttpRequest or (IDB Request?). There will be no getAttribute or nodeName property.

While there must be a better way, this will get the name of the event's target: targetname = Object.getPrototypeOf(ev.target).constructor.name.

For example, in the case of an http request, it returns "XMLHttpRequest".

Herodotus answered 23/10, 2022 at 22:52 Comment(0)
S
0

You can try:

event.target.checkBoxName.checked

where checkbox html like:

<input type="checkbox" name="checkBoxName">

It will return true or false.

var isChecked = event.target.checkBoxName.checked;

Supermarket answered 14/2, 2023 at 6:46 Comment(1)
I think the question was about checking if an event came from a checkbox or a radiobutton, but your answer is about checking if a checkbox is checkedLeucopenia
S
-1

you can use

function test(e)
{
   var eve = e || window.event;
   var ele = eve.target || eve.srcElement;

   switch(ele.getAttribute('type'))
   {
     case "radio" : //do whatever

     case "checkbox" : //do
   }

}
Summarize answered 15/11, 2013 at 6:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.