Thanaporn
Thanaporn

Reputation: 189

jquery insert-before to text in the same DOM

Hi I'm working on inserting a text to this DOM but my try did not work like I expecting.

 <p class="doing">Peter are <a href="mysite.com">here</a></p>
<script>$("p.doing").before("I and ");</script>

I'm expecting to have result:

<p class="doing">I and Peter are <a href="mysite.com">here</a></p>

but it was :

I and 
<p class="doing">Peter are <a href="mysite.com">here</a></p>

Please kindly advise how to solve this.

Upvotes: 0

Views: 82

Answers (4)

Shreedhar
Shreedhar

Reputation: 5640

instead of before, you should use prepend, as before will insert the text into p tag with a class doing.

<!DOCTYPE html>
<html>
<head>

</head>
<body>
  <p class="doing">Peter are <a href="mysite.com">here</a></p>
<script>$(".doing").prepend("I and");</script>
</body>
</html>​

Upvotes: 0

Alex Kalicki
Alex Kalicki

Reputation: 1533

Use the jQuery prepend() method:

<p class="doing">Peter are <a href="mysite.com">here</a></p>
<script>$(".doing").prepend("I and ");</script>

DEMO

Upvotes: 0

Anthony
Anthony

Reputation: 3218

Use prepend instead.

$("p.doing").prepend("I and ");

Upvotes: 4

Quicksilver
Quicksilver

Reputation: 2700

$(".doing").html('I and '+$(".doing").html());

Upvotes: 0

Related Questions