I'm trying to extract the src of an img and then paste the src into a "a href" to wrap around the original img
I have two versions that I've messed with and can't get either to work. I think one of my problems seems to be inserting an "a href" before or around an element.
<script type="text/javascript">
var x = hdnImg.children('img').attr('src')
$( "img" ).wrap(function() {
return "<a href='" +x+ "' class="anyclass"></a>";
});
</script>
or
<script type="text/javascript">
var p = hdnImg.children('img').attr('src')
$( "img" ).before( "<a href='#' class="anyclass">" );
$( "img" ).after( "</a>" );
</script>
ultimately, this is was I'm trying to go for:
html before script:
<img src="http://a.jpg">
<img src="http://b.jpg">
html after script
<a hre="http://a.jpg" class="classy"><img src="http://a.jpg"></a>
<a hre="http://b.jpg" class="classy"><img src="http://b.jpg"></a>
I'm still learning javascript and could use a little guidance. Thanks