Reputation: 196449
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
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
Reputation: 336
$('.link').each(function () {
$(this).text($(this).text() + ' ' + $(this).attr('title'));
});
Upvotes: 1
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
Reputation: 13511
Use that:
jQuery(document).ready(
function($)
{
if($('.link').text() == '[x]')
{
$('.link').text('[x] Remove');
}
}
);
Upvotes: 0
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