1

How would i go about adding text to all my current captions

<caption><span>Existing Caption Text</span></caption>
<caption><span>Existing Caption Text2</span></caption>
<caption><span>Existing Caption Text3</span></caption>
<caption><span>Existing Caption Text4</span></caption>

So i want to add text to each caption and have it shown first in the order

result would be

<caption><span>NEW TEXT Existing Caption Text</span></caption>
<caption><span>NEW TEXT Existing Caption Text2</span></caption>
<caption><span>NEW TEXT Existing Caption Text3</span></caption>
<caption><span>NEW TEXT Existing Caption Text4</span></caption>

i know how to replace the text using , but i just want it added to the existing.

$('caption span').text('NEW TEXT');

3 Answers 3

3

You can provide the text() method with a function can be used to amend the existing text value of the element. Try this:

$('caption span').text(function(i, text) {
    return 'NEW TEXT ' + text;
});
Sign up to request clarification or add additional context in comments.

Comments

0

YOu can do it using .each in jquery

$('caption span').each(function() {
    $(this).html('NEW TEXT ' +$(this).html());
});

This should work...

Comments

0

You can even use this:

$('caption span').html(function(i, text) {
    return 'NEW TEXT ' + text;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.