A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://api.jquery.com/html/ below:

.html() | jQuery API Documentation

Get the HTML contents of the first element in the set of matched elements or set the HTML contents of every matched element.

.html()Returns: String

Description: Get the HTML contents of the first element in the set of matched elements.

This method is not available on XML documents.

In an HTML document, .html() can be used to get the contents of any element. If the selector expression matches more than one element, only the first match will have its HTML content returned. Consider this code:

1

$( "div.demo-container" ).html();

In order for the following <div>'s content to be retrieved, it would have to be the first one with class="demo-container" in the document:

1

2

3

<div class="demo-container">

<div class="demo-box">Demonstration Box</div>

The result would look like this:

1

<div class="demo-box">Demonstration Box</div>

This method uses the browser's innerHTML property. Some browsers may not return HTML that exactly replicates the HTML source in an original document. For example, Internet Explorer sometimes leaves off the quotes around attribute values if they contain only alphanumeric characters.

Additional Notes: Example:

Click a paragraph to convert it from html to text.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

text-decoration: underline;

<script src="https://code.jquery.com/jquery-3.7.1.js"></script>

<b>Click</b> to change the <span id="tag">html</span>

to a <span id="text">text</span> node.

This <button name="nada">button</button> does nothing.

$( "p" ).on( "click", function() {

var htmlString = $( this ).html();

$( this ).text( htmlString );

Demo: .html( htmlString )Returns: jQuery

Description: Set the HTML contents of each element in the set of matched elements.

The .html() method is not available in XML documents.

When .html() is used to set an element's content, any content that was in that element is completely replaced by the new content. Additionally, jQuery removes other constructs such as data and event handlers from child elements before replacing those elements with the new content.

Consider the following HTML:

1

2

3

<div class="demo-container">

<div class="demo-box">Demonstration Box</div>

The content of <div class="demo-container"> can be set like this:

1

2

$( "div.demo-container" )

.html( "<p>All new content. <em>You bet!</em></p>" );

That line of code will replace everything inside <div class="demo-container">:

1

2

3

<div class="demo-container">

<p>All new content. <em>You bet!</em></p>

As of jQuery 1.4, the .html() method allows the HTML content to be set by passing in a function.

1

2

3

4

$( "div.demo-container" ).html(function() {

var emphasis = "<em>" + $( "p" ).length + " paragraphs!</em>";

return "<p>All new content for " + emphasis + "</p>";

Given a document with six paragraphs, this example will set the HTML of <div class="demo-container"> to <p>All new content for <em>6 paragraphs!</em></p>.

This method uses the browser's innerHTML property. Some browsers may not generate a DOM that exactly replicates the HTML source provided. For example, Internet Explorer prior to version 8 will convert all href properties on links to absolute URLs, and Internet Explorer prior to version 9 will not correctly handle HTML5 elements without the addition of a separate compatibility layer.

To set the content of a <script> element, which does not contain HTML, use the .text() method and not .html().

Note: In Internet Explorer up to and including version 9, setting the text content of an HTML element may corrupt the text nodes of its children that are being removed from the document as a result of the operation. If you are keeping references to these DOM elements and need them to be unchanged, use .empty().html( string ) instead of .html(string) so that the elements are removed from the document before the new string is assigned to the element.

Examples: Example 1

Add some html to each div.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

<script src="https://code.jquery.com/jquery-3.7.1.js"></script>

$( "div" ).html( "<span class='red'>Hello <b>Again</b></span>" );

Demo: Example 2

Add some html to each div then immediately do further manipulations to the inserted html.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

<script src="https://code.jquery.com/jquery-3.7.1.js"></script>

$( "div" ).html( "<b>Wow!</b> Such excitement..." );

.append( document.createTextNode( "!!!" ) )

Demo:

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4