20 Useful jQuery Methods Part -4

20 Useful jQuery Methods Part -4

16 replaceAll()
Replace each target element with the set of matched elements

If you’d like to replace DOM elements with other ones, here’s how to do it. We can call replaceAll() on elements we’ve collected or created, passing in a selector for the elements we’d like to replace. In this example, all elements with the error class will be replaced with the span we’ve created.

17 serialize()

Encode a set of form elements as a string for submission.

The serialize() method is what to use for encoding the values in a form into a string.

18 siblings()

Get the siblings of each element in the set of matched elements, optionally filtered by a selector.

You can probably guess what the siblings() method does; it will return a collection of the siblings of the whatever items are in your original collections:

19 wrap()

Wrap an HTML structure around each element in the set of matched elements.

The .wrap() function can take any string or object that could be passed to the $() factory function to specify a DOM structure. This structure may be nested several levels deep, but should contain only one inmost element. A copy of this structure will be wrapped around each of the elements in the set of matched elements. This method returns the original set of elements for chaining purposes.

Wrap a new div around all of the paragraphs.

20. find()

Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.

Given a jQuery object that represents a set of DOM elements, the .find() method allows us to search through the descendants of these elements in the DOM tree and construct a new jQuery object from the matching elements. The .find() and .children() methods are similar, except that the latter only travels a single level down the DOM tree.

The first signature for the .find()method accepts a selector expression of the same type that we can pass to the $() function. The elements will be filtered by testing whether they match this selector. The expressions allowed include selectors like > p which will find all the paragraphs that are children of the elements in the jQuery object.

Starts with all paragraphs and searches for descendant span elements, same as $( “p span” )

You may also like

Leave a Reply

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