Hướng dẫn innerhtml w3schools

Example

Get the HTML content of an element with id="myP":

let html = document.getElementById("myP").innerHTML;

Try it Yourself »

Change the HTML content of an element with id="demo":

document.getElementById("demo").innerHTML = "I have changed!";

Try it Yourself »

Get the HTML content of a

    element with id="myList":

    let html = document.getElementById("myList").innerHTML;

    Try it Yourself »

    Delete the HTML content of a

    element with id="demo":

    element.innerHTML = "";

    Try it Yourself »

    More examples below.


    Definition and Usage

    The innerHTML property sets or returns the HTML content (inner HTML) of an element.

    The Differences Between innerHTML, innerText and textContent

    See below


    Syntax

    Return the innerHTML property:

    Set the innerHTML property:

    Property Value

    Property Description
    String HTML content.

    Return Value

    Type Description
    String The HTML content of the element.


    More Examples

    Example

    Change the HTML content of two elements:

    let text = "Hello Dolly.";
    document.getElementById("myP").innerHTML = text;
    document.getElementById("myDIV").innerHTML = text;

    Try it Yourself »

    Example

    Repeat the HTML content of an element:

    element.innerHTML += element.innerHTML;

    Try it Yourself »

    Example

    Change the HTML content and URL of a link:

    element.innerHTML = "W3Schools";
    element.href = "https://www.w3schools.com";

    Try it Yourself »


    The Differences Between innerHTML, innerText and textContent

    The innerHTML property returns:
    The text content of the element, including all spacing and inner HTML tags.
    The innerText property returns:
    Just the text content of the element and all its children, without CSS hidden text spacing and tags, except


    Try it Yourself »

    Example explained:

    • The HTML document above contains a

      element with id="p1"

    • We use the HTML DOM to get the element with id="p1"
    • A JavaScript changes the content (innerHTML) of that element to "New text!"

    This example changes the content of an

    element:

    Example



    Old Heading


    Try it Yourself »

    Example explained:

    • The HTML document above contains an

      element with id="id01"

    • We use the HTML DOM to get the element with id="id01"
    • A JavaScript changes the content (innerHTML) of that element to "New Heading"


    Changing the Value of an Attribute

    To change the value of an HTML attribute, use this syntax:

    document.getElementById(id).attribute = new value

    This example changes the value of the src attribute of an

    Hướng dẫn innerhtml w3schools


    Try it Yourself »

    Example explained:

    • The HTML document above contains an
      Hướng dẫn innerhtml w3schools

      Start the Exercise