JavaScript Output Tutorial

JavaScript Display Possibilities

JavaScript can “display” data in different ways and here we discuss innerHTML and document.write.

  • Writing into an HTML element, using innerHTML.
  • Writing into the HTML output using document.write().

Using innerHTML

To access an HTML element, JavaScript can use the document.getElementById(id) method. The id attribute defines the HTML element. The innerHTML property defines the HTML content:

Example:

Using document.write()

For testing purposes, it is convenient to use document.write():

The write() method writes HTML expressions or JavaScript code to a document. The write() method is mostly used for testing: If it is used after an HTML document is fully loaded, it will delete all existing HTML.

Example:

Example

Write some text directly to the HTML document, with a new line after each statement (using <br>):

document.write(“Hello World! <br>”);
document.write(“Have a nice day!”);

Example

Write HTML elements with text directly to the HTML document:???

document.write(“<h1>Hello World!</h1><p>Have a nice day!</p>”);

Example

Write the Date object directly to the HTML:

document.write(Date());

Take Your Free Live Demo Class Now

We Will Call Back You Shortly

Enter Your DETAILS BELOW

We Will Send You the Course Fees Details In Below Number

Enter Your DETAILS BELOW

We Will Call Back You Shortly

Enter Your DETAILS BELOW

We Will Call Back You Shortly

Enter Your DETAILS BELOW

Enter Your DETAILS BELOW TO take

free demo class