Nishant Nawarkhede
Nishant Nawarkhede

Reputation: 8400

Append text to anchor tag having specific class

for this html list,

<ul class="buttons">
    <li class="back"><a href="#">ABCDE</a></li>
    <li class="back"><a href="#">B</a></li>
    <li class="back"><a href="#">CEDFRD</a></li>
    <li class="back"><a href="#">D</a></li>
    <li>E</li>
    <li>F</li>
    <li>G</li>
    <li>H</li>
</ul>

Want append text to the end of the text in anchor tag only if,

How to do this using jquery?

Upvotes: 1

Views: 5376

Answers (3)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67187

Use the receiver function of .text() to accomplish your task.

Try,

$('li.back a').text(function(_,text){ 
   return text + "yourNewText"; 
});

DEMO

As per your new edit(only if the text in anchor text is having lenght more than 5 char) you can use,

$('li.back a').text(function(_,text){ 
    return (text.length > 5) ? text + "yourNewText" : text; 
});

DEMO I

Upvotes: 1

Milind Anantwar
Milind Anantwar

Reputation: 82231

use .append():

$('li.back a').append('sm text');

Upvotes: 3

Felix
Felix

Reputation: 38102

You can use append():

$('li.back a').append('text');

Fiddle Demo

Upvotes: 5

Related Questions