jQuery - Dom Manipulation

Add an element

  • The HTML
  <p class="beauty-blue">The first beauty-blue paragraph</p>
  <p>The second paragraph</p>
  • The CSS for the class
.beauty-blue { color: blueviolet; }
  • The Javascript script with jQuery After function.
  • We select all P and we add a paragraph after.
$('p').after('<div>After</div>');
  • With a callback
$('p').after( function() {
  if (this.className != ""){
      return "<div>The class name of the previous element is " + this.className + "</div>";
  } else {
     return "<div>The previous element has no class name</div>";
  }
});
  • The result:





Discover More
DOM - Child Nodes of a Node

Child Nodes of an element in the dom tree Via child selector, you can select element that are child of another element. The childNodes property of an element returns a collection of child Node....



Share this page:
Follow us:
Task Runner