JQuery provides some standard functions through which developer can easily add element/content to an existing element. User can add an element after or before an existing element by using jQuery functions. These element can also be added using callback functions. Just check some value and according to condition, we can add element easily.
Following are jQuery methods that are used to add new content:
$("#lblComment").append("last comments");
This function will add specified text after the existing text of selected label.
$("#lblComment").prepend("start comments");
This function will add specified text before the existing text of selected label. We can use both these methods to add multiple content by using multiple parameters. These functions can easily get multiple parameters as shown below:
$("#lblComment").prepend("one", "two", "three");
$("#lblComment").append("eight", "nine", "ten");
$("#lblComment").after("after adding text");
$("#lblComment").before("before adding text");
Both these functions will add the specified text after and before the selected label element respectively. Same as append and prepend, these functions can also be used for adding multiple elements to an existing.
In above examples, we have added only texts only but we can add any other element in place of that text. Just write the element’s html code and place as parameter of these functions, that element will add easily. In further article we will remove the added element/content and how to use/append css classes through jQuery code fragment.
Following are jQuery methods that are used to add new content:
Append method
This function insert content or new element at the end of selected html element. For example$("#lblComment").append("last comments");
This function will add specified text after the existing text of selected label.
prepend()
This function insert content or new element at the beginning of selected html element. For example$("#lblComment").prepend("start comments");
This function will add specified text before the existing text of selected label. We can use both these methods to add multiple content by using multiple parameters. These functions can easily get multiple parameters as shown below:
$("#lblComment").prepend("one", "two", "three");
$("#lblComment").append("eight", "nine", "ten");
After() and before()
After method inserts content after the selected html element and before method inserts content before the selected html element. More can be explained by following jQuery code fragment:$("#lblComment").after("after adding text");
$("#lblComment").before("before adding text");
Both these functions will add the specified text after and before the selected label element respectively. Same as append and prepend, these functions can also be used for adding multiple elements to an existing.
In above examples, we have added only texts only but we can add any other element in place of that text. Just write the element’s html code and place as parameter of these functions, that element will add easily. In further article we will remove the added element/content and how to use/append css classes through jQuery code fragment.