0

Hi I have a div with content like this

 <div><strong>some content
 ....</strong></div>

How can I add another element to the div after <strong>, how to add <span> element after <strong> ?

Thank you

3 Answers 3

6

Just use append on your div element:

$('#divId').append('<span>Hi</span>');

It will insert the span element inside the div, at the end of the child node list.

Edit: In response to your comment, to remove it, since you added the element with append, you can get it selecting the last-child:

$("#divId span:last-child").remove();

Or you could remove all the span elements within the div:

$("#divId span").remove();
Sign up to request clarification or add additional context in comments.

3 Comments

Gr8 it works, how can I remove the span element? after I added it ?
Sorry, but, is there any reason for wrapping '<span>Hi</span>' in a jQuery object? You can append straight HTML...
@J-P: Yes, you're right, my fingers are getting too much used to type $() :-) edited...
1

If the <div> is the only one on the HTML page in question:

$('div strong').after('<span>Span element</span>')

See http://docs.jquery.com/Manipulation/after#content

Comments

0

append and after are easily found in the documentation

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.