Ferro Bonfiglio
Ferro Bonfiglio

Reputation: 33

Put text before string Jquery

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

Answers (4)

Guffa
Guffa

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

Adil Shaikh
Adil Shaikh

Reputation: 44740

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

Upvotes: 2

PSL
PSL

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

Test Fiddle

Upvotes: 3

Selvakumar Arumugam
Selvakumar Arumugam

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

Related Questions