How can i change html page using javascript?


The HTML DOM allows JavaScript to change the content of HTML elements.


Changing HTML Content

The easiest way to modify the content of an HTML element is by using the innerHTML property.

To change the content of an HTML element, use this syntax:

document.getElementById(id).innerHTML = new HTML

This example changes the content of a

element:

Example


Hello World!


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

How can i change html page using javascript?


Try it Yourself »

Example explained:

  • The HTML document above contains an
    How can i change html page using javascript?

    Start the Exercise




    Learn how to redirect to another webpage using JavaScript.


    Redirect a Webpage

    There are a couple of ways to redirect to another webpage with JavaScript. The most popular ones are location.href and location.replace:

    Example

    // Simulate a mouse click:
    window.location.href = "http://www.w3schools.com";

    // Simulate an HTTP redirect:
    window.location.replace("http://www.w3schools.com");

    Try it Yourself »

    Note: The difference between href and replace, is that replace() removes the URL of the current document from the document history, meaning that it is not possible to use the "back" button to navigate back to the original document.



    Introduction :

    You can add one piece of JavaScript code with a HTML page to change or modify HTML and CSS values dynamically. HTML is a markup language that is used to define the structure of a page, CSS is style rules used to add styles to a HTML page and JavaScript is a scripting programming language that is used to add complex dynamic features to a HTML page like responding to different user interactions, changing the web page dynamically, animate the page, etc. Without JavaScript, your web page will be a static page.

    Changing HTML page content :

    To change one part of a HTML page dynamically, the JavaScript code should get access to these elements. For that, each web browser provides one API called DOM or Document Object Model API. Using this API, we can dynamically change the HTML or css part of a web page.

    Inside a JavaScript code, we can access an HTML element using a document object. This object is available by default and you don’t have to create it. It provides different methods to access and modify HTML components. Following are the most commonly used DOM APIs :

    - document.getElementById("id");
    - document.getElementsByTagName("tag");
    - document.createElement(<type>);

    getElementById finds and returns one object using the provided id. Similarly, getElementsByTagName gets one element using a tag and createElement creates one element.

    All of these methods return one object that can be hold in a JavaScript variable. We can get the content of an element by using innerHTML property.

    These are common methods. We have a lot more different methods to change and manipulate each element we got.

    JavaScript program to change HTML page :

    Let’s consider the below program :

    <!DOCTYPE html>
    <html>
      <body>
        <p id="content">Default Content</p>
    
        <button id="button" type="button">Click Here</button>
      </body>
      <script>
        function changeContent() {
          document.getElementById("content").innerHTML = "Hello World!";
        }
    
        document.getElementById("button").onclick = function () {
          changeContent();
        };
      </script>
    </html>

    Create one html file index.html, copy-paste these code and open that file in a browser. It will show one button ‘Click Here’ and one text ‘Default Content’. If you click on the button, it will change the text to ‘Hello World!’.

    Explanation :

    • The id of the text that we need to change is content. The id of the button is ‘button’.
    • The JavaScript part is written inside the script tags. Once we click on the button, it calls the function ‘changeContent’.
    • This function gets the text by the id ‘content’ and changes its content using ‘innerHTML’ property to ‘Hello World!’.

    How can i change html page using javascript?

    How can I change HTML file using JavaScript?

    JavaScript Can Change Content of HTML page: The getElementById() method is used to get the id of element and change the HTML content.

    Can you replace HTML with JavaScript?

    One very useful ability provided by JavaScript is the ability to replace the content of elements in an existing HTML document that was previously displayed, even empty elements. There are many reasons that a web designer may choose to take advantage of this ability but providing interactive pages is a likely goal.

    How do you replace an HTML page?

    Use . replace() method on HTML element and replace it with the new HTML Document(eg.. $('html').

    Can JavaScript change all the HTML elements in the page?

    JavaScript can change all the HTML elements in the page. JavaScript can change all the HTML attributes in the page. JavaScript can change all the CSS styles in the page. JavaScript can remove existing HTML elements and attributes.