I have a jquery function(This is not written by me anyway still I am learning). In that we are replacing urls using f.attr("href") in several places. I am not understanding that from where this href value will be binded. And why the value(f.attr("href")) is changing place to place. I mean to say it is having some value @ one location and if I give the same it is giving me different value at other location.
I read in article that The .attr() method gets the attribute value for only the first element in the matched set..What is the matched set means
2
Let me modify my question like this: Can I append query string in Jquery function? I want to add two more querystring elements from webpage to get full url. If we can..How can we accomplish that?
.replace("${my_email}", "mailto:my@u.com?subject=Thanku &body=Jquery working.%0A%0ahttp://www.my.site.com/_layouts/products/New.aspx?url=" + f.attr("href")) In this the f.attr("href") is giving me different value than others where we used..Basically it is giving me half of the link only..
Is there any thing that you need I can provide you Thank u
When you use a selector in a jQuery call it may match more than one element. The results are returned as a jQuery object, which may contain a collection (array) of matched objects. Some methods, like
attronly work on the first element in the collection.For example,
will match all anchor tags in your document.
Now each of these will (may) have an
hrefattribute, but if you apply theattrfunction to the results of that selector, it will only operate on the first element in the collection. If I have the following HTML.and use
Then the
hrefvariable will have#0as it’s value despite the fact that the selector matches both of the anchors.Making it somewhat more confusing is that usually the version of the method that assigns values, rather than read values, will work on all of the elements in the collection. For example,
will assign
footo the title attribute of both anchors in the collection. It’s important to understand the differences in the way the various methods operate. See the jQuery API for more details on specific methods.In your case, I suspect that
fis a variable that matches different selectors in different locations in the code (fis a lousy variable name, btw, making it more verbose would probably help in understanding what the code does). That would explain why it has different values for thehrefattribute in different locations. Of course, the values could also be changing precisely because you’re setting them, but I would hope you would have understood if that were happening.