Created
November 4, 2011 17:31
-
-
Save seamusjr/1339939 to your computer and use it in GitHub Desktop.
Test if argument passed is an HTMLElement or not using JQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="foo">foo</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isHTMLElement(el){ | |
console.log($(el)[0] instanceof HTMLElement); | |
} | |
var myDiv = $('<div></div>'); | |
var arr = [,myDiv]; | |
isHTMLElement(myDiv); /* True: passing a string that conforms to HTMLElement */ | |
isHTMLElement($('.foo')); /* True: passing a selector that exists in the DOM */ | |
isHTMLElement($('.bar')); /* False: passing a selector that doesn't exist in DOM */ | |
isHTMLElement('string'); /* False: passing a string that is not a HTMLElement */ | |
isHTMLElement('<html></html>'); /* True: passing a string that conforms to HTMLElement */ | |
isHTMLElement(function(){var x = "y"}); /* False: passing a function */ | |
isHTMLElement(arr[0]); /* False: undefined value in arr[0] */ | |
isHTMLElement(arr[1]); /* True: reference to myDiv that contains valid HTMLElement */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment