Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Output

JavaScript Output


JavaScript has no built-in print or output function.

JavaScript displaying data

JavaScript can output data in different ways:


Using window.alert()

You can pop up an alert box to display data:

Example

<

!DOCTYPE

html

<

html

<

body

<

h1

My first page

<

/h1

<

p

My first paragraph.

<

/p

<

script

window.alert(5 + 6);

<

/script

<

/body

<

/html

Try it »


Manipulating HTML elements

To access an HTML element from JavaScript, you can use the document.getElementById(id) method.

Use the "id" attribute to identify the HTML element, and innerHTML to get or insert the element's content:

Example

<

!DOCTYPE

html

<

html

<

body

<

h1

My first web page

<

/h1

<

p

id=

"demo"

My first paragraph

<

/p

<

script

document.getElementById("demo").innerHTML = "Paragraph changed.";

<

/script

<

/body

<

/html

Try it »

The above JavaScript statement (in