CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   Javascript   Javascript PrependChild – How to Prepend New Element W ...

Javascript PrependChild – How to Prepend New Element With jQuery ?

August 17, 2017 by SNK

prepend child using jquery

If you have noticed javascript prependchild doesnot exists which is opposite of appendChild. So in this tutorial we will see how to prepend new html element just before the first child of the parent element.

If you want to add new child element of the parent element what you generally do is, use the appendChild() method.

Lets make a structure of an element to make this more clear. lets construct the div with its parent and child elements :-

HTML
1
2
3
4
5
<div id="parentElement">
<div id="childElement1">1</div>
<div id="childElement2">2</div>
<div id="childElement3">3</div>
</div>

In avobe code if you want to append any child in the parentElement, you just have to do this :-

AppendChild
JavaScript
1
2
3
4
5
6
7
8
// First Create a new element
var newElement = document.createElement("span");
 
// then save a parent element in a varibale
var parentElement = document.getElementById("parentElement");
 
// Then just use the appendMethod()
parentElement.appendChild(newElement);

Its very easy and javascript have provided this method so that you can do it very easily by using its inbuilt method.

If you inspect element and check in elements section you should have <span> element as the bottom of parentElement as its childElement.

append child after the parent element using javascript

But, what if you want to append child just before the first child element of an parent element ? Well javascript haven’t provided any methods for it.

So we need to use the jQuery insertBefore() method. Lets see how :-

Inserting before first child of an parent element
JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Create new element that you want to insert
var newElement = document.createElement("span");
 
// Now get the parent element
var parentElement = document.getElementById("parentElement");
 
//Getting the child of parents
var ParentChilds = parentElement.childNodes;
 
// Get the first child (In 0 index of childNodes we have the first child of an parent element)
var firstchild = parentElement.childNodes[0];
 
// lets insert new node before first child of an parent element
parentElement.insertBefore(newElement,firstchild);

Now, again inspect element and check, we have successfully placed new element before first child element of the parent element.

Javascript Prepend Child - Add New Element Just Before First Child of the Parent Element

If you want to view all the child nodes of an current parent element then you can use this code below to view and debug.

view childs of parent elements
JavaScript
1
2
3
4
5
6
7
/*
If you want to see all the child elements of parentElement
then you do can do with the help of this code below
*/
 
var totalChilds = parentElement.querySelectorAll("*");
console.log(totalChilds);

You have successfully placed the new element before the first child of parent element. Now, what if you can create a function for it like javascript and use it all over other place you want ?

Lets create a function for it so that you can use it across all the projects you want.

Creating a library function for code Reuse
JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// First create the element you want
var newElement = document.createElement("a");
 
// now the parent element where you want to insert
var parentElement = document.getElementById("parentElement");
 
// Now the function you and copy it somwhere in your scripts as an library
function prependChild(newElement,parentElement) {
var firstchild = parentElement.childNodes[0];
parentElement.insertBefore(newElement,firstchild);
}
 
// Now you just have to call the function and pass the arguments and its done.
prependChild(newElement,parentElement);

This it, we have successfully created our own custom function to prepend the element using jQuery and javascript. If you think you have benefited from this tutorial be sure to like us on facebook.

SHARE ON
Buffer

Enjoyed this article?

Like us on

Javascript javascript prepend without jquery javascript prependchild javascript prependto

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.