jquery manipulating HTML elements (adding elements)

Source: Internet
Author: User

Add a new HTML content

We'll learn the four jQuery methods for adding new content:

    • Append ()-inserts content at the end of the selected element
    • Prepend ()-Inserts content at the beginning of the selected element
    • After ()-Inserts the content after the selected element
    • Before ()-insert content before selected element
JQuery Append () method

The JQuery append () method inserts the content at the end of the selected element.

Instance
$ ("P"). Append ("Some appended text.");
JQuery prepend () method

The JQuery prepend () method inserts content at the beginning of the selected element.

Instance
$ ("P"). Prepend ("Some prepended text.");
Adding several new elements through the Append () and prepend () methods

In the example above, we only insert the text/html at the beginning/end of the selected element.

However, the Append () and prepend () methods can receive an unlimited number of new elements through parameters. You can use JQuery to generate text/html (as in the example above), or through JavaScript code and DOM elements.

In the following example, we create a number of new elements. These elements can be created by text/html, JQuery, or javascript/dom. We then append these new elements to the text (as valid for prepend () using the Append () method:

Instance
function AppendText () {var txt1= "<p>Text.</p>";               Create a new element in HTML var txt2=$ ("<p></p>"). Text ("text.");   Create new element var txt3=document.createelement ("P") with JQuery;  Create a new element in DOM txt3.innerhtml= "Text."; $ ("P"). Append (TXT1,TXT2,TXT3);         Append new Element}
JQuery after () and before () methods

The JQuery after () method inserts content after the selected element.

The JQuery before () method inserts content before the selected element.

Instance
$ ("img"). After ("Some text after"); $ ("img"). Before ("Some text before");
Adding several new elements through the After () and before () methods

The After () and before () methods can receive an unlimited number of new elements through parameters. You can create new elements through text/html, JQuery, or javascript/dom.

In the following example, we create several new elements. These elements can be created by text/html, JQuery, or javascript/dom. We then use the After () method to insert these new elements into the text (as valid for Before ()):

Instance
function Aftertext () {var txt1= "<b>i </b>";                    //Create a new element in HTML var txt2=$ ("<i></i>"). Text ("Love");     //Create new element var txt3=document.createelement ("Big") through JQuery  ; //Create new elements via DOM txt3.innerhtml= "jquery!"; $ ("img"). After (TXT1,TXT2,TXT3);          //Insert new element after img}   

jquery manipulating HTML elements (adding elements)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.