hey take a look at this code:
$("li,p").click(function(){ // how do I perform a test, and know wich Element it is ? if( /* I'm a <li/> */ ) // Do this for the <li/> if( /* I'm a <p/> */ ) // Do this for the <p/> })
thanks guys ^^
One option is to use the is () method :
$("li,p").click(function(){ // how do I do I perform a test, on wich Element it is ? if($(this).is('li')) // Do this for the <li/> if($(this).is('p')) // Do this for the <p/> })
You are looking for this.nodeName.
this.nodeName
You can also write
if (jQuery.nodeName(this, 'p'))
Live demo
$("li,p").click(function(){ if(this.tagName.toLowerCase() == "p"){ alert("PP!"); }else if(this.tagName.toLowerCase() == "li"){ alert("list!"); } })
Use function is()
is()
$("li,p").click(function(){ if( $(this).is('li') ) // Do this for the <li/> if( $(this).is('p') ) // Do this for the <p/> });