Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

99
Views
How to append an element after specific child element using jquery?

I want to append a paragraph element after specific div class element.

I want to add a p element inside second text div class. I tried a solution but it's not working.

My solution it's not working. How can I insert this p element in second text div class using jquery ?

$(".text:nth-child(2)").append("<p>Five</p>");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="text">
  <p>First</p>
</div>
<div class="text">
  <p>Second</p>
</div>
<div class="text">
  <p>Third</p>
</div>
<div class="text">
  <p>Fourth</p>
</div>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

nth-of-type this seems to work better

$(".text:nth-of-type(2)").append("<p>Five</p>");
.text { border: 1px solid black; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="text">
  <p>First</p>
</div>
<div class="text">
  <p>Second</p>
</div>
<div class="text">
  <p>Third</p>
</div>
<div class="text">
  <p>Fourth</p>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

You can use .eq(n)

$(".text").eq(1).append("<p>Five</p>");
.text {
  padding: 5px;
  margin-bottom: 10px;
  background: #f0f0f0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="text">
  <p>First</p>
</div>
<div class="text">
  <p>Second</p>
</div>
<div class="text">
  <p>Third</p>
</div>
<div class="text">
  <p>Fourth</p>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

Just in case you don't necessarily need jQuery, you can go with vanilla JS aswell.

var el = [...document.getElementsByClassName("text")][1];
var p = document.createElement("p");
var t = document.createTextNode("Five");
el.appendChild(p.appendChild(t));
<div class="text">
  <p>First</p>
</div>
<div class="text">
  <p>Second</p>
</div>
<div class="text">
  <p>Third</p>
</div>
<div class="text">
  <p>Fourth</p>
</div>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!