Reputation: 87
I have been trying to wrap a link around an image. I have searched and found that wrap()
in jQuery would help me. When I tried it, nothing seems to work. Is there something that I am doing wrong?
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="http://a1.mzstatic.com/us/r30/Music3/v4/fb/af/59/fbaf5908-0839-abc6-9f6a-bc7cc5b84f27/cover170x170.jpeg" class="YTimgs">
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript' />
<script>
$('.YTimgs').wrap('<a href="http://www.chordzone.org"></a>');
</script>
Upvotes: 0
Views: 110
Reputation: 700212
The problem is that you are missing the closing tag for the script that loads jQuery, so the script tag after it will be swallowed up and ignored.
A script tag can not be self-closed, so the second script tag ends up part of the first one, and as there is a src
attribute in the script tag, the content inside the tag is ignored.
Add an ending tag to the script tag:
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'></script>
Upvotes: 0
Reputation: 1187
Your code seems to be ok as it works in this fiddle http://jsfiddle.net/pqqyoakm/ It could be that the javascript is executing before the image has loaded try:
$(document).ready(function(){
$(".YTimgs").wrap("<a href='http://www.chordzone.org'></a>'")
})
Upvotes: 1