Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery add link to text

Is there a way in JQuery to select text from the html document and add a link around it?

Many Thanks, Nav

like image 230
van Avatar asked Dec 09 '22 12:12

van


1 Answers

You can use jQuery's wrap function:

$(someSelector).wrap(function() {
   var link = $('<a/>');
   link.attr('href', 'somewhere_far_far_away');
   link.text($(this).text());
   return link;
});
like image 174
Jacob Relkin Avatar answered Jan 04 '23 21:01

Jacob Relkin