Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Hyperlink to text in JQuery

I am using innerText to add text to my object. Is there an easy way to add a hyperlink to the text? 'trend' also has a attribute called 'link'.

this.node.innerText = trend.get('value');
like image 448
Parag Srivastava Avatar asked Aug 08 '13 14:08

Parag Srivastava


2 Answers

You need to add a DOM element using jQuery's wrap():

$(this).wrap('<a href="..." />');

like image 103
Diodeus - James MacFarlane Avatar answered Nov 17 '22 13:11

Diodeus - James MacFarlane


Use **WRAP** function

  $(someSelector).wrap(function() {
       var link = $('<a/>');
       link.attr('href', 'somewhere_far_far_away');
       link.text($(this).text());
       return link;
    });
like image 33
Vaibs_Cool Avatar answered Nov 17 '22 11:11

Vaibs_Cool