Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between $.add and $.append JQuery

I was wondering and couldn't get any best documentation that what's the difference between $.add and $.append when we have single element to add or append to a container.

Thanks in Advance

like image 749
Zahid Riaz Avatar asked Jun 05 '12 07:06

Zahid Riaz


People also ask

What is the difference between append and after in jQuery?

append insert the parameter element inside the selector element's tag at the last index position, whereas the . after puts the parameter element after the matched element's tag.

What does append mean jQuery?

In jQuery, the append() method is used to insert specified content as the last child (at the end of) the selected elements in the jQuery collection.

What is difference between append and appendTo in jQuery?

The append (content) method appends content to the inside of every matched element, whereas the appendTo (selector) method appends all of the matched elements to another, specified, set of elements.

Is append JavaScript or jQuery?

jQuery append() Method The append() method inserts specified content at the end of the selected elements. Tip: To insert content at the beginning of the selected elements, use the prepend() method.


2 Answers

They are not at all related.

.add()

Add elements to the set of matched elements.

e.g.

If you want to do,

$('div').css('color':'red'); $('div').css('background-color':'yellow'); $('p').css('color':'red'); 

Then, you can do,

$('div').css('background-color':'yellow').add('p').css('color':'red'); 

Reference

.append()

Insert content, specified by the parameter, to the end of each element in the set of matched elements.

$('div').append('p'); 

will append selected p on all selected div in dom.

Reference

like image 197
Jashwant Avatar answered Oct 09 '22 04:10

Jashwant


Given a jQuery object that represents a set of DOM elements, the .add() method constructs a new jQuery object from the union of those elements and the ones passed into the method. But it does not insert the element into the DOM, i.e using .add() the element will be added to the DOM but to see it in the page you have to insert it in the page using some insertion/append method.

like image 21
Kamran Ali Avatar answered Oct 09 '22 04:10

Kamran Ali