jQuery add new element and html

jQuery add new element and html


jQuery add() method will create a new jQuery object with elements added to the matched elements.

jQuery add()


jQuery add() method is very useful if you want to add new html content to DOM elements. For example: ajax load content to content container.

Here is examples show how we use add() to add new DOM element:
<div id="foo">Hello</div>
jQuery code:
$("foo").add("<span>Again</span>")
The result:

[ <p>Hello</p>,<span>Hello Again</span> ]

There are other 4 methods that are used to add new content:
append,prepend,after,before.

methoddescription
append(content)Insert content to the end of each element in the set of matched elements.
appendTo()Insert every element in the set of matched elements to the end of the target.
prepend(content)Insert content to the beginning of each element in the set of matched elements.
prependTo()Insert every element in the set of matched elements to the beginning of the target.
after(content)Insert content after each element in the set of matched elements.
before(content)Insert content before each element in the set of matched elements.
date2019-04-02 20:46:41 From:www.jquerypluginhub.com Author:JPH
Related Tags:
Comments

Leave a Reply

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