views:

89

answers:

2

Possible Duplicate:
Can jQuery provide the tag name?

Hi!

This question is so basic i am ashamed asking but i tried to find the answer for 30 minutes without any result.

How do i find out what kind of element has been clicked in the code below.

$('*').click(function (event) {
 var this_element = $(this).???;
 return false;
})

What i am looking for is to have the this_element variable set to 'a' if it's a link, 'p' if it's a paragraph 'div' if...

Thanks!

+5  A: 

Try this:

$('*').click(function (event) {
    var this_element = this.tagName.toLowerCase();
    return false;
});

The this pointer refers to the actual element being acted upon. As part of the DOM Level 2 core, all DOM elements have a property called .tagName.

Xavi
Be sure to `.toLowerCase()` some browsers implement all caps for `.tagName` while others don't.
gnarf
It really depends on what you're doing with the tag name. It may not matter. But fair enough, I'll make the change.
Xavi
+3  A: 
$(this).get(0).tagName;
ghoppe
um, -1 for what? it works: http://jsbin.com/akohe/edit
ghoppe
@ghoppe: donno, but it is somewhat redundant - `$(this).get(0)` should always equal `this`.
Shog9
only true in this specific case (clicking on an element). $(this) may contain more than one DOM element…
ghoppe
Well, sure... If `this` happened to be a selector string, or an array of elements, or some such. But that's not really relevant to *this* question.
Shog9