Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross browser solution for checking if a Javascript object is an html element

The following code work in FF but not IE8:

var j = "test";
alert(j instanceof HTMLElement);

I don't think IE uses the HTMLElement object. Is there a safe way to do this check in IE? Perhaps there is a YUI solution?

like image 398
stevebot Avatar asked Apr 08 '26 18:04

stevebot


2 Answers

I use to check the nodeType property, it should be 1 for all HTML element objects.

I used it in my crossbrowser library before I switched to jQuery.

text has a nodeType of 3 and any custom objects probably won't have a nodeType property at all.

like image 100
David Mårtensson Avatar answered Apr 11 '26 07:04

David Mårtensson


If you look at the jQuery source code (not the minified one ffs!), you'll see they make use of nodeType a lot.

like image 22
Christian Avatar answered Apr 11 '26 09:04

Christian