How to check if an object is a div or whether?

Using jquery or JS, how to check if an object is div or li?

+3
source share
5 answers

Using jQuery:

jQuery(someElement).is('li'); // => return true if it an <li>
jQuery(someElement).is('div'); // => returns true if it a <div>
+6
source

Check the tagName property.

+4
source

:

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

( IE, ?) tagName nodeName. , , .

+4

tagName

+2

JS, , :

function whichTag(el) {
  return el && el.tagName && el.tagName.toLowerCase();
}

:

whichTag(document.getElementById('link')); //return 'a'
whichTag(document.getElementById('list')); //return 'li'
whichTag(document.getElementById('side')); //return 'div'

jQuery, is(); :

$('#link').is('a'); //return true
$('#list').is('li'); //return true
$('#side').is('div'); //return true

, , , null.

0

Source: https://habr.com/ru/post/1740441/


All Articles