LemmyLogic
LemmyLogic

Reputation: 1083

Add a span tag to text inside a hyperlink, using jQuery

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

Right now, I'm changing the existing text to something else, using this:

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

I need the link to look like this when it is parsed:

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

So I need to add that span tag inside the

Any ideas?

Upvotes: 1

Views: 11842

Answers (2)

Silver Light
Silver Light

Reputation: 45922

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

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

Upvotes: 2

nickf
nickf

Reputation: 546045

$("a:contains('text')")
    .text('new-text')
    .append($('<span></span>')
        .addClass('someclass')
        .text('some other text')
    )
;

Upvotes: 8

Related Questions