Script text Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Script


The text property sets or gets the contents of the <script> element.

Set the text property with the following Values

contents Sets the contents of the script

Return Value

A String, representing the contents of the script.

Returns all the text nodes that are children of the <script> element in tree order.

The following code shows how to get the contents of the <script> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>

<p id="fruitdemo"></p>

<button onclick="fruitFunction()">Remove the last array</button>

<script id="myScript">
var fruits = ["A", "B", "C", "D"];

function fruitFunction() {//from   ww  w. j a  v  a 2s.c  o m
    var x = document.getElementById("fruitdemo");
    x.innerHTML = fruits;

<p id="contentsdemo"></p>

<button onclick="textFunction()">Return contents</button>

function textFunction() {
    var x = document.getElementById("myScript").text;
    document.getElementById("contentsdemo").innerHTML = x;


Related Tutorials