- jQuery html()
- Syntax
- Working of jQuery html()
- Examples to Implement jQuery html()
- Example #1
- Example #2
- Example #3
- Example #4
- Example #5
- Example #6
- Example #7
- Example #8
- Conclusion
- Recommended Articles
- jQuery – title attribute
- Read title attribute
- Writing attribute title
- jQuery — DOM Manipulation
- jQuery DOM Manipulation
- jQuery — Get Content
- Example
- Get Form Fields
- Example
- jQuery — Set Content
- Example
- Set Form Fields
- Example
- jQuery — Replacement Elements
- Example
- jQuery HTML/CSS Reference
jQuery html()
The html() method can be used to provide the content of any element in an HTML document. If the match expression meets more than one element, its HTML content is returned only to the first match. This jQuery html method takes advantage of the innerHTML property of the browser. Several browsers might not return HTML, which simulates the exact source of HTML in an actual document.
Web development, programming languages, Software testing & others
Some jQuery constructor or method which acknowledges an HTML string can effectively execute script through code. It can happen by inserting script identifiers or using code implementing HTML attributes. Don’t use such mechanisms to inject strings collected from untrusted sources such as parameters for the URL query, cookies or inputs for the form. By doing so, this can incorporate vulnerabilities by cross-site scripting (XSS). Delete or escape input from any user prior to adding content to document
Syntax
1. It generates the first matched item content s shown below:
2. It provides matched element content, as shown below:
3. The content is set using a function, as shown below:
$(selector).html(function(index, currentcontent))
Parameters in jQuery html()
This method takes two parameters, as mentioned above:
1. Content: This is the mandatory element that defines the new content for the chosen elements.
2. Function(index, currentcontent): This is an optional parameter that defines a function that returns the specified elements’ new content.
- Index: The index position of the element inside the set is returned.
- currentcontent: The current HTML content of the selected element is returned.
Working of jQuery html()
The html() method works with the above three parameters based on the criteria. The method html () selects or returns the content or innerHTML of the elements chosen. While using this method to return content, it provides the first matched element content. Whenever this process is used for setting content, the content of all matched elements is overwritten.
Examples to Implement jQuery html()
Below is the example of implementing in jQuery html() methods:
Example #1
html () example
- Save the above file with a .html extension and run it in the web browser. It will display the screen, as shown below:
- Click on the button shown in the result. When you click on the button, it will give the below result:
Example #2
html () example
Example #3
html () example
Example #4
.color2 < color:blue; >.color1 html () example
First data.
Second data.
- When you run the above example, you would get the content of the first-line, and it will get the display in the second line:
Example #5
.txt < margin: 10px; font-size: 15px; color: red; cursor: pointer; >.txt1 Click on this here to change the html to text.
Example #6
.txt Hello World .
Example #7
div
Example #8
.div Click on the given boxes to see the respective box number:
This is simple text .
This is first box .
This is second box .
This is third box .
Conclusion
So far, we have studied about html () method in jQuery. While using the HTML (,) method to determine an element’s content, any content stored in that element will be entirely replaced by new content. Moreover, jQuery eliminates other constructs from child elements, including data and event handlers, before exchanging such elements with the new content. This method basically uses the innerHTML property of JavaScript. The internalHTML property sets and gets the matched element of the HTML content.
Recommended Articles
This is a guide to jQuery html(). Here we discuss Syntax, methods, parameters, and examples to implement jQuery html() with proper codes and outputs. You can also go through our other related articles to learn more –
jQuery – title attribute
To manipulate the title attribute, you can use the standard attr() method. Using this method, you can read the current value of the title attribute, or you can write a new value.