jquery: test whether input variable is dom element -
i write jquery function accepts either dom element or id input:
function myfunction(myinput){ // pseudocode: // if (myinput dom element){ // var myid = $(myinput).attr('id'); // } else { // var myid = myinput; // } // stuff myid ... }
question: how can tell whether myinput dom element???
it's easier check other way around - check if it's string if use id else treat dom node/element , handle if one.
function myfunction(myinput) { var myid; if (typeof myinput == 'string'){ myid = myinput; } else { myid = myinput.id; // myinput.id enough } // }
or if want check against if it's htmlelement every dom html element extends htmlelement abstract interface. check mdc more info on htmlelement.
... if (myinput instanceof htmlelement){ myid = myinput.id; // myinput.id enough } else { myid = myinput; } ...
in end won't matter... call!
tom
Comments
Post a Comment