Jquery what is wrap element. Manipulating Page Elements with jQuery

Now we will study working with XML.

XML is a format for exchanging data between sites.

It is very similar to HTML, but XML allows its own tags and attributes.

Why is XML needed for parsing? Sometimes it happens that the site that you need to parse has an API with which you can get what you want without much effort.

Therefore, just a piece of advice - before parsing a site, check whether it has an API.

What is an API? This is a set of functions with which you can send a request to this site and receive the desired response.

This answer most often comes in XML format. So let's start studying it.

Working with XML in PHP

Let's say you have XML. It can be in a string, or stored in a file, or returned upon request to a specific URL.

Let the XML be stored in a string. In this case, you need to create an object from this string using new SimpleXMLElement :

$str = "Kolya 25 1000"; $xml = new SimpleXMLElement($str);

Now we have an object with parsed XML stored in the $xml variable. By accessing the properties of this object, you can access the contents of the XML tags. We’ll look at how exactly below.

If the XML is stored in a file or sent via a URL (which is most often the case), then you should use the simplexml_load_file function, which makes the same $xml object:

Kolya 25 1000

Let the XML be stored in a string. In this case, you need to create an object from this string using new SimpleXMLElement :

$xml = simplexml_load_file(path to file or URL);

Working methods

In the examples below, our XML is stored in a file or URL.

Let the following XML be given:

Let the XML be stored in a string. In this case, you need to create an object from this string using new SimpleXMLElement :

Working methods

Let's get the employee's name, age and salary:

$xml = simplexml_load_file(path to file or URL); echo $xml->name; //will display "Kolya" echo $xml->age; //will print 25 echo $xml->salary; //will output 1000

As you can see, the $xml object has properties corresponding to the tags.

You may have noticed that the tag does not appear anywhere during the request.

This is because it is the root tag.

You can rename it, for example, to - and nothing will change:

There can only be one root tag in XML, just like a tag in regular HTML.

Number 1

$xml = simplexml_load_file(path to file or URL); echo $xml->worker["name"]; //will display "Kolya" echo $xml->worker["age"]; //will output 25 echo $xml->worker["salary"]; //will output 1000 echo $xml->worker; //will display "Number 1"

Tags with hyphens

XML allows tags (and attributes) with a hyphen. In this case, accessing such tags occurs like this:

Kolya Ivanov

$xml = simplexml_load_file(path to file or URL); echo $xml->worker->(first-name); //will display "Kolya" echo $xml->worker->(last-name); //will display "Ivanov"

Looping

Let us now have not one employee, but several.

In this case, we can iterate over our object using a foreach loop:

Kolya 25 1000 Vasya 26 2000 Petya 27 3000

$xml = simplexml_load_file(path to file or URL); foreach ($xml as $worker) ( echo $worker->name; //will display "Kolya", "Vasya", "Petya" )

From object to normal array

If you're not comfortable working with the object, you can convert it to a normal PHP array using the following trick:

$xml = simplexml_load_file(path to file or URL); var_dump(json_decode(json_encode($xml), true));

More information Parsing based on sitemap.xml

Often a site has a sitemap.xml file.

This file stores links to all pages of the site for ease of indexing by search engines (indexing is essentially site parsing by Yandex and Google).

In general, we shouldn’t worry much about why this file is needed, the main thing is that if it exists, you don’t have to crawl through the pages of the site using any tricky methods, but simply use this file.

How to check the presence of this file: let us parse the site site.ru, then go to site.ru/sitemap.xml in the browser - if you see something, then it’s there, and if you don’t see it, then alas.

If there is a sitemap, then it contains links to all pages of the site in XML format. Calmly take this XML, parse it, separate links to the pages you need in any way convenient for you (for example, by analyzing the URL, which was described in the spider method).

As a result, you get a list of links for parsing; all you have to do is go to them and parse the content you need.

Read more about the sitemap.xml device on Wikipedia.

What should you do next:

Start solving problems using the following link: problems for the lesson.

When you decide everything, move on to studying a new topic.

If you need to wrap each matched element, or elements in a set, with selected HTML elements, then you can use the .wrap() method.

jQuery syntax: 1.2 syntax: $(selector).wrapAll( wrappingElement) Syntax 1.4: $(selector).wrapAll(function ) Prior to jQuery 3.0, the function was called incorrectly for each element in the set, it received the index of the element in the set as a parameter (similar to the .wrap() method).

Added in jQuery 1.2 (syntax updated in 1.4) Parameter values ​​Usage example Using the jQuery .wrapAll() method (wrapping an HTML element)

.violetBorder /* set a solid purple border of 1 pixel size */ )

$("p ").wrapAll(" "); // wrap all elements

In the set ) );

) );

Cry

Regular paragraph

Second regular paragraph Third regular paragraph.

.wrapAll()

in a set of an element with a class .violetBorder A similar result can be achieved when creating a DOM element:

$("p").wrapAll(

document.createElement

("div"));

The result of our example:

Note that you can create a complex nested structure by wrapping the required elements with multiple HTML elements: Using jQuery's .wrapAll() method (wrapping multiple HTML elements)

div ( border : 1px solid violet ; )

$(document).ready(function ()( $("button ").click(function ()( // set the function when the element is clicked $("span ").wrapAll("

"); // wrap all elements in the set ) ) ) ); Click Normal span

$("p").wrapAll(

Regular paragraph

Second regular span

In this example, using jQuery's .wrapAll() method, when a button is clicked, we wrap all the elements in the document with several HTML elements. Notice how the elements are grouped and how the flow of the document has changed.

Consider the following example in which we pass a function as a parameter to the .wrapAll() method.

In this example, using jQuery's .wrapAll() method, we wrap all elements when a button is clicked

HTML element manipulation allows you to refine, expand a wrapped set of elements by adding new content, or remove elements from the set. To manipulate elements, they must be selected using selectors or selection methods.

Element Set Management 1. Adding content to the page 1.1. Method.html()

Returns the HTML content of the first element of the wrapped set, or appends HTML content to each element of the set.

Html() The method is specified without parameters. Returns the contents of the first element in the matching set as HTML markup.

.html (markup fragment) markup fragment - adds a fragment of HTML markup to the content of all elements of the corresponding set.

.html(function) function - takes two arguments: the index of the element and the current content of the element. The return value is added as new content.

1.2. Method.text()

Returns the combined text content of all elements of the wrapped set, including their children, or adds new text content.

Text() The method is specified without parameters. Concatenates the text content of all wrapped elements and returns the resulting text as a result that can be stored in a variable.

.text(string) string - Sets the contents of the parameter to be the new text content of all wrapped elements, removing the old content. If the line contains angle brackets, they are replaced with equivalent HTML elements.

.text(function) function - Called for each element of the wrapped set. The function takes two arguments - the index of the element and the current content of the element. The return value will be added as new content.

The method is similar to .append() with the difference that the jQuery function is passed the element to be added rather than the wrapped set. Adds the elements of the wrapped set after the contents of the elements specified by the parameter.

AppendTo(the element to which content is appended)

2.3. Method.prepend()

Works similarly to .append() , only the new content is added at the beginning, immediately after the element's opening tag, before the element's content.

Prepend(content1, content2) content1 - Adds an HTML element, array, string, or jQuery object as content.

content2 - optional parameter, defines additional content, one or more HTML elements, arrays, strings or jQuery objects are added.

.prepend(function) function - called for each element of the set, the function is passed two arguments - the index of the element and the current contents of the element. The function returns a string, DOM element, or jQuery object. The returned value will be used as content to complement the element's existing content.

2.4. Method.prependTo()

The method is similar to .prepend() with the difference that the jQuery function is passed the element to be added rather than the wrapped set. Adds the elements of the wrapped set to the beginning of the contents of the elements specified by the parameter.

PrependTo(element to which content is added) The element to which content is added - a selector, HTML element, element array, string, or jQuery object is added to the element.

2.5. Method.before()

Adds the HTML fragment or element specified in the method parameter to the DOM tree, before each element of the wrapped set. The function is called for each element of the set, it is passed the element itself and two arguments - the index of the element and the current contents of the element. The returned value will be used as content to complement the element's existing content.

Inserts the content passed to the jQuery function before each element specified as an argument to this method.

InsertBefore(element to which content is added) The element to which content is added - a selector, HTML element, array of elements, string, or jQuery object is added to the element.

2.7. Method.after()

Adds the content specified in the method parameter to the DOM tree after each element of the wrapped set.

After(content1, content2) content1 - Adds an HTML element, array, string, or jQuery object as content.

content2 - optional parameter, defines additional content, one or more HTML elements, arrays, strings or jQuery objects are added.

.after(function) function - the index of the element in the set and its current contents are passed as an argument to the function. The function returns a string, DOM element, or jQuery object. The returned value will be used as the content to be added.

.after(function) function - the index of the element in the set is passed as an argument to the function. The function returns a string, DOM element, or jQuery object. The returned value will be used as the content to be added.

2.8. Method.insertAfter()

Inserts the content passed to the jQuery function after each element specified as a parameter to this method.

InsertAfter(element to which content is added) The element to which content is added - a selector, HTML element, array of elements, string, or jQuery object is added to the element.

2.9. Method.wrap()

The method wraps an element or group of elements with HTML markup. If the set contains multiple elements, each of them will be wrapped.

WrapAll(wrapper element) wrap element is a string representing a piece of HTML markup, selector, element with a given class, or jQuery function that will wrap a set of elements.

.wrapAll(callback function) callback function - called once for each element of the set. Receives the index of the element in the set as an argument. Inside the function, this refers to the current element in the set. Returns the DOM element, JQuery object, or HTML fragment in which the corresponding element will be wrapped.

2.11. Method.wrapInner()

Wraps the contents of the set elements, including text content, in the specified HTML markup.

WrapInner A wrapper element is a string representing a piece of HTML markup, a selector, an element with a given class, or a jQuery function that will wrap the contents of a set of elements.

.wrapInner(callback function) callback function - called once for each element of the set. Receives the index of the element in the set as an argument. Inside the function, this refers to the current element in the set. Returns a DOM element, JQuery object, or HTML fragment in which the contents of the corresponding element will be wrapped.

3. Replacement and removal of elements 3.1. Method.unwrap()

Removes the element that wraps the set.

Unwrap() The method is called without parameters.

3.2. Method.detach()

Removes all elements of the wrapped set from the DOM. Keeps data and events associated with elements intact. All child elements are also excluded from the DOM. Excluded elements and their associated data can later be returned to the DOM tree.

Detach(selector) selector

3.3. Method.remove()

Completely removes all elements of the wrapped set from the DOM. In this case, you can delete several elements with the same class. All child elements are also excluded from the DOM. At the same time, all data and events associated with them are deleted.

Remove(selector) selector is an optional parameter that specifies which elements are to be removed.

3.4. Method.empty()

Removes the contents of all elements of a set, removing all content as well as all child elements within it.

Empty() No arguments are passed to the method.

ReplaceWith(new content) new content - specifies the content to be inserted. Can be a piece of HTML markup with or without content, a selector, an array of elements, or a jQuery object.

.replaceWith(function) function - the return value will replace each element of the wrapped set.

3.6. Method.replaceAll()

Replaces each element that matches the selector passed to the method with a wrapped set of elements. Returns a wrapped set containing the replaced elements. Replaced elements are removed and cannot participate in subsequent operations.

ReplaceAll(target element) The target element is a selector, JQuery object, HTML element, or array of elements to be replaced.

3.7. Method.clone()

Creates copies of the elements in the wrapped set. Elements are copied along with their nested elements. The resulting copies of elements can then be appended to another location in the DOM for further processing.

Clone() boolean - specifies whether elements' event handlers should be copied along with them. The default value is false . If set to true , event handlers will be copied along with the elements.

You can rename it, for example, to - and nothing will change:

This lesson covers working with attributes, properties, and advanced jQuery selectors.

Now we will learn how to read, add and change attributes of HTML tags using jQuery.

All this is done using the .attr() method. It works similar to the .html() method - depending on the number of parameters it will read or change the value of the attribute.

The first parameter of .attr() is the name of the attribute, and the second is its new value.

For example, like this - .attr("value", "www") - we will write the string "www" to the value attribute.

If we call .attr() with one parameter, it will return the values ​​of the attribute whose name we specify in the first parameter.

For example, like this - .attr("value") - we will get the value of the value attribute.

In the following example, we get an input by its id and display the value of its value attribute:

alert($("#test").attr("value")); //will print "!!!"

Now let’s write the new value “www” to the same attribute:

$("#test").attr("value", "www");

Removing an attribute

For attributes like disabled and checked, the .attr() method is not suitable (such attributes are called “properties”). For them, you need to use the .prop() method, which takes the attribute name as the first parameter, and true or false as the second.

If set to true, the attribute will be installed, and if false, the attribute will be removed.

Let's set the disabled attribute for the input:

$("#test").prop("disabled", true);

The HTML code will look like this:

Now, on the contrary, let’s remove disabled:

$("#test").prop("disabled", false);

The HTML code will look like this:

You can not only change the values ​​of such property attributes, but also read them:

alert($("#test").prop("disabled")); //will display true - the attribute exists

value forms

To work with the value attribute of inputs, you can also use the .val() method, which allows you to read and write new values.

Let's write the new value "www" to the value attribute:

$("#test").val("www");

The HTML code will look like this:

Now let's display the current value on the screen:

alert($("#test").val());

There is one more nuance - for the textarea tag you cannot get the internal content through the .html() method.

To do this you need to use the .val() method:

!!!

alert($("#test").val()); //will print "!!!"

Most likely, this was done to ensure uniformity in working with forms.

Working with classes

Let me remind you that in the class attribute in HTML code you can write several classes separated by a space.

Let's imagine this situation - you want to add a new class to the existing ones without overwriting them. In this case, simply doing .attr("class", "new-class") will not work - you will overwrite the classes that are already in the attribute.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

In the following example, an element that already has two classes - class="www zzz" - is also added with the class kkk .

The HTML code will look like this:

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

As a result, the contents of the attribute will look like this - class="www zzz kkk" :

Paragraph text.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

$("#test").addClass("kkk");

The HTML code will look like this:

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

A similar method exists for removing a class - .removeClass() .

Let's do this, for example: when you click the button for the first time, the class red will be added to the element, which adds the red color, and when you click it again, this class will be removed and the red color will disappear. If you press the button again, everything will happen again.

Try running the following code and see for yourself (the zzz class is italicized to show that toggleClass will not interfere with other classes' work):

Red ( color: red; ) .zzz ( font-style: italic; )

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

$("#test").toggleClass("red");

After the first click, the HTML code will look like this (the red class will be added):

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

And after the second click - like this (the red class will disappear):

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

Well, the last method of working with classes that we will look at is called .hasClass() .

It checks the presence or absence of the class passed to it as a parameter. If the element has such a class, it will print true, and if there is no such class, it will print false.

Wrapping elements

Tags can be wrapped in other tags, for example, if we have paragraphs, then we can additionally wrap them in tags.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

For such things, the .wrap() method is used, which allows you to wrap each element found in a specified tag.

The HTML code will look like this:

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

For example, let's wrap all paragraphs with the www class with the tag.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

To do this, we will pass the string “div” as a parameter to the method, like this - .wrap("div").

The HTML code will look like this:

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

Look what we can come up with:

$(".www").wrap("div");

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

To do this, we will pass the string “div” as a parameter to the method, like this - .wrap("div").

The HTML code will look like this:

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

As a parameter, you can pass not only the name of the tag, but also the following construction - "" - in this case the effect will be exactly the same:

$(".www").wrap("");

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

Of course, the second method - "" - is less convenient, however, it has an advantage - you can write any attributes into the opening tag (in our case div), and the wrapping will be along with these attributes.

The HTML code will look like this:

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

Let's wrap our paragraphs in a div with the zzz class:

On the contrary, you can not wrap the element, but unwrap it (that is, remove the parent) using the .unwrap() method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

In the following example, we have paragraphs that are inside . Let's perform the unwrap operation for paragraphs with the www class:

The HTML code will look like this:

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

$(".www").unwrap();

Let's find all the paragraphs with the class www and wrap them all in one tag:

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

$(".www").wrapAll("div");

The HTML code will look like this:

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

Please note that if the wrapped elements are not adjacent, the .wrapAll() method will first move the elements to one place and then wrap them. See the following example:

Text of paragraph 1.

Text of paragraph 2.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

Text of paragraph 3.

$(".www").wrapAll("div");

The HTML code will look like this:

Text of paragraph 1.

Text of paragraph 2.

Text of paragraph 3.

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

Inserting elements

There are 4 methods for inserting elements in jQuery: .append(), .prepend(), .after(), .before().

The .prepend() method allows you to add text to the beginning of elements:

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

$("p").prepend("!!!");

The HTML code will look like this:

!!!Paragraph text.

The .append() method allows you to add text to the end of elements:

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

$("p").append("!!!");

The HTML code will look like this:

Paragraph text.!!!

The .before() method allows you to add text in front of elements:

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

$("p").before("!!!");

The HTML code will look like this:

!!!

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

The .after() method allows you to add text after elements:

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

$("p").after("!!!");

The HTML code will look like this:

The .addClass() method allows you to add a class to an element (or elements) without losing other classes. The class you want to add to the element is taken as a parameter to this method.

!!!

There are also alternative forms of methods for inserting elements. For example, instead of .append() you can use .appendTo() .

The difference is in the method of application: if append takes as a parameter the text to be inserted, then appendTo, on the contrary, takes as a parameter a selector of elements for which append should be applied.

The following entries are equivalent:

$(".www").append("!!!") $("!!!").appendTo(".www")

There are also other equivalent methods. Here they are:

Methods .prepend() and .prependTo() :

$(".www").prepend("!!!") $("!!!").prependTo(".www")

Methods .before() and .insertBefore() :

$(".www").before("!!!") $("!!!").insertBefore(".www")

Methods .after() and .insertAfter() :

$(".www").after("!!!") $("!!!").insertAfter(".www")

Replacing elements

You can replace some elements with others using the .replaceWith() method. Let's replace all paragraphs with the class www with !!! .

Last update: 11/1/2015

In the previous paragraph, we inserted child elements into an already existing element. But we can also create a new element and wrap existing ones in it. The wrap() method and the similar wrapAll() and wrapInner() methods are used for this. For example, we may need to wrap some elements in a new div element. For this we resort to the above methods.

wrap method

The wrap method wraps the elements of the selection with the element that is passed as an argument to this method. It has the following use cases:

    wrap("html markup") : wraps the selection elements in an element created from html markup

    wrap(element) : wraps the selection elements in the html element passed as a parameter

    wrap("selector") : wraps the selection elements in the document element matching the selector

    wrap(jQuery) : Wraps the selection elements in a jQuery object

    wrap(function) : wraps the selection elements in the object returned by the function

Let's say we have the following markup:

  • Java
  • C/C++

Let's wrap the list using the wrap method:

$(".langs").wrap("");

  • Java
  • C/C++

Similarly, we can use the wrap method in other ways:

Var header = document.getElementById("header");

$(".langs").wrap(header);

Note that in this case the list is not inserted into an existing element, but simply the existing element is cloned and the list is wrapped in this copy.

wrapAll method

    To wrap all the elements of the selection as a whole into one element, use the wrapAll method. It has similar use cases:

    wrapAll("html markup") : Wraps all selection elements into a single element generated from the html markup

    wrapAll(element) : wraps all elements of the selection into a single html element passed as a parameter

    wrapAll("selector") : wraps all elements of the selection into a single element matching the selector

wrapAll(jQuery) : Wraps all selection elements into a single jQuery object

For example, we have the following markup:

Java C/C++ PHP

Let's wrap all these elements into one div element:

$(".lang").wrapAll("");

After this, the markup will look like this:

Java C/C++ PHP

wrapInner method

    To wrap the contents of elements into a new element, use the wrapInner method. It has the following use cases:

    wrapInner("html markup") : wraps the contents of the selection elements in an element created from the html markup

    wrapInner(element) : wraps the contents of the selection elements in the html element passed as a parameter

    wrapInner("selector") : Wraps the contents of the selection elements into the document element matching the selector

    wrapInner(jQuery) : wraps the contents of the selection elements in a jQuery object

wrapInner(function) : wraps the contents of the selection elements in the object returned by the function

  • Java
  • C/C++

The initial markup will look like this:

Wrap the contents of list elements in tags

$("li.lang").wrapInner("");