leora
leora

Reputation: 196449

How can i change the text inside a link with jQuery

I have the following link HTML:

  <a href="#" onclick="run(1); return false;" class="link" title="Remove item">[x]</a>

and i want to use jquery to change this to be

  <a href="#" onclick="run(1); return false;" class="link" title="Remove item">[x] Remove Item</a>

I want to keep the link and all of the attributes but change the text inside the link

Upvotes: 3

Views: 11468

Answers (6)

Riz
Riz

Reputation: 10236

$('.link').text($('.link').text() + 'Remove Item');

OR

$('.link').each(function(){
    $(this).text($(this).text() + 'Remove item');
});

OR

$('.link').each(function(){
    $(this).text($(this).text() + $(this).attr('title'));
});

Upvotes: 2

chrispanda
chrispanda

Reputation: 3224

$("a.link").append('Remove item')

Upvotes: -1

mre666
mre666

Reputation: 336

$('.link').each(function () {
    $(this).text($(this).text() + ' ' + $(this).attr('title'));
});

Upvotes: 1

Lapple
Lapple

Reputation: 3373

Try this:

var $link = $('a[title="Remove item"]');
$link.text($link.text()+ ' ' + $link.attr('title'));

MORE: The following is more obvious, but far less universal:

$('a[title="Remove item"]').text('[x] Remove item');

Upvotes: 1

KodeFor.Me
KodeFor.Me

Reputation: 13511

Use that:

jQuery(document).ready(
    function($)
    {
        if($('.link').text() == '[x]')
        {
           $('.link').text('[x] Remove');
        }
    }
);

Upvotes: 0

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103135

You can get the text in the link with the text() function and set it back with the text(String) function like this:

 $(".link").text($(".link").text() + " Remove Item");

And you can give your link a unique id if you wish to target just a single link rather than all elements with .link class.

Upvotes: 7

Related Questions