0

I want add text in a div element that already has a text. I used .append() but this will put the text after the text that already is inside. I want to append the text before the text that already is inside. =)

But how?

4 Answers 4

3

Use .prepend to add before the existing content.

.prepend( content [, content ] ) - Insert content, specified by the parameter, to the beginning of each element in the set of matched elements.

Sign up to request clarification or add additional context in comments.

Comments

3

You are looking at prepend()

Example:-

$('selectorContainer').prepend('Text to appear first or element selector or element itself inside the selectorContainer');

See Doc

Test Fiddle

Comments

2
$('div').prepend("something");

Comments

1

If the element contains only text, get the text from the element, concatenate it with your text, and put it back. Example:

$('#me').text('Prefix' + $('#me').text());

If you want to add it as an element, use the prepend method:

$('#me').prepend('Prefix');

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.