Add span tag to text inside hyperlink using jQuery

How would you add a span tag to text inside a hyperlink?

Now I am changing the existing text to something else using this:

$(document).ready(function() { $("a:contains('text')").text('new-text') }); 

I need a link to look like this when it parses:

 <a href="/xxx.aspx">new-text<span class="someclass">some other text</span></a> 

So I need to add this span tag inside

Any ideas?

+1
source share
2 answers
 $("a:contains('text')") .text('new-text') .append($('<span></span>') .addClass('someclass') .text('some other text') ) ; 
+5
source

you can use the html() method to add markup:

 $(document).ready(function() { $("a:contains('text')").html('new-text<span class="someclass">some other text</span>') }); 
+1
source

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


All Articles