Tuesday 24 September 2019

javascript - How can I tell the difference between a regular object and a jquery object (page element)?





// var = {hey: "baby"};
// or
// var = $('#thingy');

if(typeof var == 'object'){ // this is true for both =(

}


I need my code to be smart enough to be able to tell the difference between these two. What's the best way to accomplish that?


Answer



You can use the instanceof operator for this.



obj instanceof jQuery



In addition, I don't think you want to use "var" as a variable name. It's reserved to establish variable scope.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...