0

there's a site with lots of comments.

code1
<p>hello ernie</p>
<p>this is mark</p>
<p>hello ben</p>
<p>this is ernie</p>
<p>hello mark</p>
<p>this is ben</p>

Now I want to collect all comments containing 'hello' and insert the phrases with hello after all comments.

$("p:contains('hello')").insertAfter($("#comments"));

'hello' is taken from the comments and inserted after the comments.

code2
<p>this is mark</p>
<p>this is ernie</p>
<p>this is ben</p>
code3
<p>hello ernie</p>
<p>hello ben</p>
<p>hello mark</p>

Is there a way to keep the comments as they were (code1) and still insert all comments containing 'hello' after the comments. Like

How it should look like.


<p>hello ernie</p>
<p>this is mark</p>
<p>hello ben</p>
<p>this is ernie</p>
<p>hello mark</p>
<p>this is ben</p>

<p>hello ernie</p>
<p>hello ben</p>
<p>hello mark</p>

Any hint or help is much appreciated.

2 Answers 2

2

Use .clone() to create a copy then move only those copies, like this:

$("p:contains('hello')").clone().insertAfter("#comments");

Also .insertAfter() takes a selector directly, so no need to wrap it here.

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

Comments

1

You could use .clone() like so:

$("p:contains('hello')").clone().insertAfter($("#comments"));

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.