views:

63

answers:

4

Hi all

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?

+2  A: 

If you have several anchor tags on the page, you stand the chance of selecting more than one of them.

<a href="#">Missed</a>
<div>
    <a href="#">Anchor</a>
    <a href="#">Anchor</a>
    <a href="#">Anchor</a>
    <a href="#">Anchor</a>
    <span><a href="#">Missed</a></span>
</div>

If you did $('div > a') as your jQuery selector, you would match (in this example) all anchor tags whose contents is "Anchor"; because this selector only matches anchor tags who are children of divs.

You can see how many elements are your matched set by calling alert(f.length);

If you can provide the code that defines f, or see some of the HTML in your page, we'll be able to help you more specifically!

If you set a value using f.attr(), all matched elements will update. If you get a value using f.attr(), only the value for the first element will be returned.

Matt
alienavatar
+2  A: 

A matched set is zero and more elements which are the result of the initial query, for example $('.foo') will return a set with all elements which has the class foo defined. When you call .attr('href') on that set it will only return the attribute for the first element in the set, if any, as returning all attributes in an array or concatenates isn't what most people want usually.

If you really want all attributes, you can do as following:

var array_of_hrefs = $('.foo').each(function(){
  return $(this).attr('href')
}).get();
azatoth
+2  A: 

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 attr only work on the first element in the collection.

For example,

$('a')

will match all anchor tags in your document.

Now each of these will (may) have an href attribute, but if you apply the attr function to the results of that selector, it will only operate on the first element in the collection. If I have the following HTML.

 <a href='#0'>Zero</a>
 <a href='#1'>One</a>

and use

var href = $('a').attr('href');

Then the href variable will have #0 as 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,

$('a').attr('title','foo');

will assign foo to 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 f is a variable that matches different selectors in different locations in the code (f is 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 the href attribute 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.

tvanfosson
I am afraid to continue this thread. I can explian the scenario as much as I can. I have a pop up in a webpage and it carries some query string attributes. And that pop up has some buttons like print and email. when I gave f.attr("href") it is taking all the query string parameters. And I am giving the same f.attr("href") in the body of email. This time the link is taking only one querystring param. Is there anything issue as I am giving in email body? I am not seeing multiple href values in my code though. Do you have any ideas in this.Thank you very much for your reply
alienavatar
@alienavatar - you're going to need to show some code samples from each if you want anything specific. `f` is presumably a variable that corresponds to a selector, but we don't know what or how. Add the relevant (and complete) javascript snippet(s) to your question + the HTML if appropriate. Please don't add it as an answer -- just edit your question and append it to the bottom, marked as an edit to the question. Add a comment to each answer to let us know when you've updated the question.
tvanfosson
@tvanfosson I edited my question. Can you look at it and share your thoughts
alienavatar
@alienavatar -- that's probably better as a separate question, but the short answer is yes you can. You can use the serialize method to convert inputs into query string parameters and simply append them using string manipulation. `var url = $('a').attr('href') + '?' + $('#item_one,#item_two').serialize()`;.
tvanfosson
A: 

.replace("${my_email}", "mailto:[email protected]?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

alienavatar