Hey there! I have a bunch of classes named itemdata
and I'm looking to incorporate some jQuery within each div. Within every div, there is a link that I want to extract the href
attribute value from and enclose it within another <span>
in that specific div.
My current code structure appears as follows:
<div class="itemdata" >
<a href="www.link1" class="frame">
<img src='...' />
</a>
<span class="wlt_shortcode_TITLE">Title 1</span>
</div>
<div class="itemdata" >
<a href="www.link2" class="frame">
<img src='...' />
</a>
<span class="wlt_shortcode_TITLE">Title 2</span>
</div>
<div class="itemdata" >
<a href="www.link3" class="frame">
<img src='...' />
</a>
<span class="wlt_shortcode_TITLE">Title 3</span>
</div>
Below is my jQuery implementation:
</script>
jQuery( ".itemdata" ).each(function() {
var link = jQuery( this ).find(".frame").attr("href");
jQuery(this).find(".wlt_shortcode_TITLE" ).wrap("<a href='" + link + "'></a>");
});
</script>
The desired output HTML should appear as follows:
<div class="itemdata" >
<a href="www.link1" class="frame">
<img src='...' />
</a>
<a href="www.link1"><span class="wlt_shortcode_TITLE">Title 1</span></a>
</div>
<div class="itemdata" >
<a href="www.link2" class="frame">
<img src='...' />
</a>
<a href="www.link2"><span class="wlt_shortcode_TITLE">Title 2</span></a>
</div>
<div class="itemdata" >
<a href="www.link3" class="frame">
<img src='...' />
</a>
<a href="www.link3"><span class="wlt_shortcode_TITLE">Title 3</span></a>
</div>
However, the current HTML rendered is not quite what I intended:
<div class="itemdata" >
<a href="www.link1" class="frame">
<img src='...' />
</a>
<a href="undefined">
<a href="www.link1">
<a href="www.link1"><span class="wlt_shortcode_TITLE">Title 1</span>
</a>
</a>
</a>
</div>
<div class="itemdata" >
<a href="www.link2" class="frame">
<img src='...' />
</a>
<a href="undefined">
<a href="www.link1">
<a href="www.link1"><span class="wlt_shortcode_TITLE">Title 2</span>
</a>
</a>
</a>
</div>
<div class="itemdata" >
<a href="www.link3" class="frame">
<img src='...' />
</a>
<a href="undefined">
<a href="www.link1">
<a href="www.link1">
<span class="wlt_shortcode_TITLE">Title 3</span>
</a>
</a>
</a>
</div>