0

I am trying to figure out how to get an element type in jQuery.

If I click on something I would like the console.log to write out what kind of element I clicked, if it is a link, button, checkbox and so on.

Something like this:

console.log("time: " + Date() + "  element type: " + $.(this).type);

This does not work, except for the date which works fine.

Andreas
  • 21,535
  • 7
  • 47
  • 56
Doris
  • 1

1 Answers1

1

Use the nodeName property of the DOM object. There is no need to create a new jQuery object for this:

console.log("time: " + Date() + " element type: " + this.nodeName);

this is the element containing the click event. If you want to know which element inside it got clicked you can use the target property of the event object:

$('a').click(function(event) {
    console.log("time: " + Date() + " element type: " + event.target.nodeName);
});

For the difference between nodeName and tagName see also this question on StackOverflow.

Community
  • 1
  • 1
jazZRo
  • 1,598
  • 1
  • 14
  • 18