Reputation: 33
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?
Upvotes: 0
Views: 6372
Reputation: 700312
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');
Upvotes: 1
Reputation: 123739
You are looking at prepend()
Example:-
$('selectorContainer').prepend('Text to appear first or element selector or element itself inside the selectorContainer');
See Doc
Upvotes: 3
Reputation: 79830
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.
Upvotes: 3