Starting with this:
<div class="outside">
<div class="container">
<a href="link1"></a>
</div>
</div>
</div>
<div class="outside">
<div class="container">
<a href="link2"></a>
</div>
</div>
<div class="outside">
<div class="container">
<a href="link3"></a>
</div>
</div>
What I am trying to achieve:
<div class="outside">
<a href="link1" class="overlay">
<div class="container">
<a href="link1"></a>
</div>
</a>
</div>
<div class="outside">
<a href="link2" class="overlay">
<div class="container">
<a href="link2"></a>
</div>
</a>
</div>
<div class="outside">
<a href="link3" class="overlay">
<div class="container">
<a href="link3"></a>
</div>
</a>
</div>
Needs to work if for any number of ".container" divs. I’m guessing I should be putting the inner hrefs into an array but I’m getting stuck on how to insert the values into the overlay wraps.
Here’s one of my attempts:
var arr = $('.container a').map(function() {
return this.href;
}).toArray();
$('.outside').each(function() {
.wrapInner( '<a href=' + arr + '></a>' );
});
2
Answers
Here is a solution with Vanilla JavaScript.
Create an anchor tag
Get all the elements with className "outside"
call a for each for them
You are almost success,just need to using index to get the right link attribute