I would like to know how I could use jQuery inside a native prototype function of the String object for instance.
I’ve tried:
String.prototype.jQ = function() {
var $currentObject = $( this );
return ( $currentObject.length ) ? $currentObject.val() : this;
};
var test = "#txtEmail";
alert( test.jQ() );
With no luck. Any suggestion?
I am perfectly aware that I could use $( test ).val() but I would like to know if I can do it my way.
Thanks!
Oddly enough, jQuery only accepts primitive string values as selectors, not String objects, which is what
thisis in aStringprototype method. You can use.valueOf()to get the primitive:Here’s a demo.