Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the right selector for this jQuery function? or $("#myElement")[0].href vs $("#myElement").href

Tags:

jquery

please see: http://jsfiddle.net/4Z4fQ/19/

consider the following html:

<a id="myElement" href="http://www.google.com/">
    <img id="image"  src="http://www.google.com/intl/en_com/images/srpr/logo3w.png" alt="google"/>
</a>

and the following script:

jQuery.fn.MyFunction = function (event) {
   alert('My function was called');
   alert(this.href);
};


$("#myElement").click(function (event) {
   //some processing

   $("#myElement").MyFunction(event);
});

without changing MyFunction, how do I change this line: $("#myElement").MyFunction(event); so I get the href to show in the alert?

like image 349
Barka Avatar asked Dec 14 '25 16:12

Barka


1 Answers

Description

The selector is right. But you must call jQuery's .attr() function to get the href attribute because this is a jQuery Object.

Sample

jQuery.fn.MyFunction = function (event) {
   alert('My function was called');
   alert(this.attr("href"));
};

More Information

  • Updated jsFiddle
  • jQuery.attr()
like image 169
dknaack Avatar answered Dec 17 '25 08:12

dknaack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!