This is the current sample:
<div class="main-wrap">
<div class="inner-wrap">
some example text here
<span>
<a href="1">test1</a>
</span>
</div>
<div class="inner-wrap">
some example text here
<span>
<a href="2">test2</a>
</span>
</div>
<div class="inner-wrap">
some example text here
<span>
<a href="3">test3</a>
</span>
</div>
</div>
I want to retrieve the href attribute
of each a tag
inside the span tag
and add it to the entire inner-wrap
element. Here's an expected outcome:
<div class="main-wrap">
<div class="inner-wrap">
<a href="1">
some example text here
<span>
<a href="1">test1</a>
</span>
</a>
</div>
<div class="inner-wrap">
<a href="2">
some example text here
<span>
<a href="2">test2</a>
</span>
</a>
</div>
<div class="inner-wrap">
<a href="3">
some example text here
<span>
<a href="3">test3</a>
</span>
</div>
</a>
</div>
I am able to extract the href attribute of each a tag
but I'm struggling with how to insert it into the respective inner-wrap
divs. This is my current progress:
$('.inner-wrap').each(function(){
$this = $(this);
$fetchLink = $this.children('span').find('a').attr('href');
console.log($fetchLink);
});
Although this task might seem simple, I'm unable to find a solution on my own. Any assistance would be appreciated.
View the functioning jsfiddle demo