2

I need to create a function that gets the url from one link and applies it to another within the same div. I can get it to loop through but it always assigns the last variable to all of the links.

Here is the code :

$.fn.getLink = function() { 

    $('a.firstlink').each(function(){

         var linkHref= $(this).attr('href');

         ('a.secondlink').attr('href', linkHref)

    });

}

$('div.containing-links').each(function(){

    $(this).getLink();

});

Help appreciated.

Thanks for your quick answers, to make it a bit simpler here is the code that adds the link correctly but repeats the last href variable to all of them:

$('a.firstlink').each(function(){

var linkHref= $(this).attr('href');

$('a.secondlink').attr('href', linkHref);

    });

How can I apply this to each one at a time?

1
  • Can you post HTML in question. It's kind of hard to get what you are aiming at. Commented Jan 24, 2011 at 12:52

2 Answers 2

2

Your problem is that $('a.secondlink') grabs all of the links on the page. just because it is inside the each loop does not mean it is scoped to anything.

Just change $('a.secondlink').attr('href', linkHref) to look something like $(this).siblings('a.secondlink').attr('href', linkHref);

Sign up to request clarification or add additional context in comments.

6 Comments

This is not working for me but I think you have got the right idea that its applying the href to all links. When I alert(linkHref); it goes through each one sucessfully.
overlooked that one, this is the correct answer though there is a better way to do a search in particular context $('a.firstLink', this) you can pass context for element search as a 2nd parameter for jquery selector.
@Tom, that is a nice feature, but since he's looping through links, not containers, it's not necessarily viable.
@areid my example probably doesn't work because I don't know your DOM structure, and the links are not direct siblings. The concept remains the same, you just need to figure out the correct traversal method to the link you want (instead of using the .siblings() method.)
@Stephen it is viable as he is replacing links which are wrapped by a div container div.containing-links on which he calls the .each() method to replace these links in every of these wrappers - check my answer below where I've put your answer fix with the use of context on the structure I believe he is aiming at.
|
1

I don't think this is a good spot to write a plugin. You should do it straightforward in your .each() loop.

$('div.containing-links').each(function(){
    var $self    = $(this),
        $first   = $self.find('a.firstlink'),
        $second  = $self.find('a.secondlink');

    if( $first.length && $second.length )
        $second[0].href = $first[0].href;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.