Skip to content

Instantly share code, notes, and snippets.

@seamusjr
Created November 4, 2011 17:31
Show Gist options
  • Save seamusjr/1339939 to your computer and use it in GitHub Desktop.
Save seamusjr/1339939 to your computer and use it in GitHub Desktop.
Test if argument passed is an HTMLElement or not using JQuery
<div class="foo">foo</div>
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