Append ()-insert content at the end of the selected element •
Prepend ()-insert content at the beginning of the selected element •
After ()-insert content after the selected element •
Before ()-insert content before the selected element •
$("p").append("Some appended text.");
$("p").prepend("Some prepended text.");
However, the append () and prepend () methods can receive an infinite number of new elements through parameters. You can use jQuery to generate text/HTML (as in the preceding example), or use JavaScript code and DOM elements.
In the following example, we create several new elements. These elements can be created using text/HTML, jQuery, or JavaScript/DOM. Then we use the append () method
Append these new elements to the text (which is equally valid for prepend ):
Instance
Function appendText () {var txt1 = "<p> Text. </p> "; // create a new element var txt2 in HTML = $ (" <p> </p> "). text ("Text. "); // create a new element var txt3 = document with jQuery. createElement ("p"); // create a new element named txt3.innerHTML = "Text. "; $ (" p "). append (txt1, txt2, txt3); // append a new element}
The jQuery before () method inserts content before the selected element.
Instance
$("img").after("Some text after");$("img").before("Some text before");
Function afterText () {var txt1 = "<B> I </B> "; // create a new element in HTML var txt2 =$ ("<I> </I> "). text ("love"); // create a new element var txt3 = document through jQuery. createElement ("big"); // create a new element named txt3.innerHTML = "jQuery! "; $ (" Img "). after (txt1, txt2, txt3); // Insert new elements after img}
$("#div1").remove();
$("#div1").empty();
The jQuery remove () method also accepts a parameter that allows you to filter the deleted elements. This parameter can be the syntax of any jQuery selector.
The following example deletes all <p> elements of class = "italic:
$("p").remove(".italic");