HasAttribute ("id") before IE8

I used

element.hasAttribute('id') 

in my code to check if an element has an id attribute . But hasAttribute API is only compatible with browsers after IE8. There is a similar API or method that I can use to check the availability of the attribute for the element in my case, "id".

+4
source share
2 answers

Just check element.id- it will be an empty string if it is not installed.

There is no need to use element.hasAttributefor those attributes that are reflected by the properties of the JS object.

+2
source

hasAttribute getAttribute. null, , - . , , .

if ((element.getAttribute('id') === null) || (element.getAttribute('id') === '')) {
+6

All Articles