bluebyte
bluebyte

Reputation: 103

filling href from array

I'm trying to find a way to fill the href of the link in "expose" with the href of "titel".

The function correctly selects the href and writes it to console but the href of the expose is always the last element of the array. But it needs to be correctly sorted

e1 titel to e1 expose e2 titel to e2 expose ...

<div class="e1">
    <div class="titel"><a href="test.html">titel</a></div>
    <div class="expose"><a href="">expose</a></div>
</div>
<div class="e2">
    <div class="titel"><a href="test.html">titel</a></div>
    <div class="expose"><a href="">expose</a></div>
</div>
<div class="e3">
    <div class="titel"><a href="test.html">titel</a></div>
    <div class="expose"><a href="">expose</a></div>
</div>

$('.titel a').each(function() {
      var ary= $(this).attr("href");
      console.log(ary);

      $('.expose a').each(function() {          
         $(this).attr("href", "what should be written in front of the link"+ (ary) +"download.pdf");            
    });     
});

Upvotes: 1

Views: 47

Answers (2)

Brisbe
Brisbe

Reputation: 1628

Your issue is the inner loop. Instead of that loop, do something like this:

$('.titel a').each(function() {
    var ary= $(this).attr("href");
    console.log(ary);

    $(this).parent().find('.expose a').attr("href", "what should be written in front of the link"+ (ary) +"download.pdf");
});

Upvotes: 2

dfsq
dfsq

Reputation: 193261

This should do it:

$('.titel a').each(function() {
    var ary = $(this).attr("href");
    $(this).parent().next().find('a').attr('href', ary);
});

Upvotes: 2

Related Questions