Explanation of JQuery Add Elements with Example.

Add New HTML Content

We will look at four jQuery methods that are used to add new content:

  • append() – Inserts content at the end of the selected elements.
  • prepend() – Inserts content at the beginning of the selected elements.
  • after() – Inserts content after the selected elements.
  • before() – Inserts content before the selected elements.

jQuery append() Method

The jQuery append() method inserts content AT THE END of the selected HTML elements.

<script>

$(document).ready(function(){

$(“.btn-one”).click(function(){

$(“p”).append( “<b> Appended Text</b> .” );

});

$(“.btn-two”).click(function(){

$(“ol”).append( “<li> Appended list Text</li> .” );

});

});

</script>

<body>

<p >This is Roshan Kumar Jha</p>

<ol>

<li> List 1</li>

<li> List 3</li>

<li> List 3</li>

</ol>

<button type=”button” class=”btn-one” >click</button>

<button type=”button” class=”btn-two” >click me</button>

</body>

jQuery prepend() Method

The jQuery prepend() method inserts content AT THE BEGINNING of the selected HTML elements.

<script>

$(document).ready(function(){

$(“.btn-one”).click(function(){

$(“p”).prepend( “<b> Appended Text</b> .” );

});

$(“.btn-two”).click(function(){

$(“ol”).prepend( “<li> Appended list Text</li> .” );

});

});

</script>

<body>

<p >This is Roshan Kumar Jha</p>

<ol>

<li> List 1</li>

<li> List 3</li>

<li> List 3</li>

</ol>

<button type=”button” class=”btn-one” >click</button>

<button type=”button” class=”btn-two” >click me</button>

</body>

jQuery after() and before() Methods

The jQuery after() method inserts content AFTER the selected HTML elements.

The jQuery before() method inserts content BEFORE the selected HTML elements.

<script>

$(document).ready(function(){

$(“.btn-one”).click(function(){

$(“img”).before( ” Before” );

});

$(“.btn-two”).click(function(){

$(“img”).after( “After” );

});

});

</script>

<body>

<img src = images/roshan.jpg alt =”roshan” width = “100”><br>

<button type=”button” class=”btn-one” >click</button>

<button type=”button” class=”btn-two” >click me</button>

</body>

Related Posts

Advanced Certified MLOps Professional Program for Scalable AI Model Deployment Systems

Introduction The Certified MLOps Professional program from AIOpsSchool has emerged as a vital benchmark for engineers looking to bridge the gap between data science and production engineering….

Read More

Powerful Certified MLOps Engineer Program to Build Reliable ML Infrastructure

Introduction The integration of Machine Learning into production environments has created a significant gap between data science and traditional software engineering. The Certified MLOps Engineer program is…

Read More

Professional Skill Alignment Around MLOps Foundation Certification in Modern Workplaces

Introduction The MLOps Foundation Certification has emerged as a critical benchmark for professionals looking to bridge the gap between data science and production engineering. This guide is…

Read More

Certified AIOps Manager: Strategic Framework for Intelligent IT Operations

Introduction The Certified AIOps Manager program is a specialized training designed to help professionals lead the next wave of IT operations. This guide is for engineers and…

Read More

Advanced AIOps Architect Certification Roadmap for DevOps Engineers

Introduction The Certified AIOps Architect is a comprehensive professional program designed for engineers and architects who want to master the intersection of Artificial Intelligence and IT Operations….

Read More

Advanced Certified AIOps Professional Guide for Mastering AI Driven Operations Skills

Introduction Artificial Intelligence for IT Operations is the future of managing complex systems and large scale digital environments. The Certified AIOps Professional program is designed for those…

Read More