views:

63

answers:

4

Using jquery or JS how do i check if an object is a div or a li?

+4  A: 

Check it's tagName property.

Dead account
`nodeName` is a better choice.http://www.quirksmode.org/dom/w3c_core.html#t23
J-P
+2  A: 

use the tagName property

z33m
+4  A: 

Here's a quick example:

$("div, li").click(function() {
    alert(this.nodeName);
});

There are some differences (mainly IE, surprised?) between tagName and nodeName. The few that matter can be found here.

Nick Craver
+1 and deleted own.
Dead account
@Ian - Undelete! Your's is a perfectly valid answer, just posting this so people know of some odd cases between the two as well :)
Nick Craver
@Nick - done. I just hate to think of someone using something which didn't work across browsers. jQuery FTW!
Dead account
+3  A: 

Using jQuery:

jQuery(someElement).is('li'); // => return true if it's an <li>
jQuery(someElement).is('div'); // => returns true if it's a <div>
J-P