Add Anchor Tag

In my application, I want to make the image a link to another page. The image is placed in html as,

<div class="rightbuttoncontainers_footer"> <div class="forfooterimg" id="twitterlink_indv"> <img src="Images/tweatus.png" width="30" height="30" alt="tweat"> </div> </div> 

I want to make this image as links through a script, I tried the following

 var twit_lk='https://twitter.com/'; $('#twitterlink_indv').append('<a href="'+twit_lk+'"/>'); 

But the output of the above code is as follows:

 <div class="rightbuttoncontainers_footer"> <div class="forfooterimg" id="twitterlink_indv"> <img src="Images/tweatus.png" width="30" height="30" alt="tweat"> <a href="https://twitter.com/"></a> </div> </div> 

But I want the result to be as follows:

 <div class="rightbuttoncontainers_footer"> <div class="forfooterimg" id="twitterlink_indv"> <a href="https://twitter.com/"> <img src="Images/tweatus.png" width="30" height="30" alt="tweat"> </a> </div> </div> 

How can I make it work?

Please, help,

thanks

+4
source share
8 answers

Take a look at the .wrap() method registered here .

 $('#twitterlink_indv img').wrap('<a href="' + twit_lk + '" />'); 
+4
source

Use the wrap method. Select an image element and circle the link around it:

 $('#twitterlink_indv img').wrap('<a href="'+twit_lk+'"></a>'); 
+3
source

Use .wrap () , for example:

 $('#twitterlink_indv img').wrap('<a href="'+twit_lk+'"/>'); 
+2
source

to try:

 $('#twitterlink_indv img').wrap( $("<a/>").attr("href", twit_lk)); 
+2
source

try using wrap instead of append like this:

 $('#twitterlink_indv img').wrap('<a href="'+twit_lk+'"/>'); 
+1
source

I think you are looking for jQuery wrap() function.

Wrap the HTML structure around each element in the set of matched elements.

 $('#twitterlink_indv > img').wrap('<a href="' + twit_lk + '" />'); 

Your selector must be modified to match the internal <img> . The wrapper function then wraps the matched elements with an anchor tag.

Look here for a demo.

Links -

+1
source

use jquery wrap() :

 $('#twitterlink_indv > img').wrap('<a href="'+twit_lk+'"/>'); 
0
source

Just use the wrapInner() method:

 $('#twitterlink_indv').wrapInner('<a href="'+twit_lk+'"/>'); 
0
source

Source: https://habr.com/ru/post/1438391/


All Articles