In: Computer Science
Code:
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
<script type="text/javascript">
//fun() is a function that stores
the json array
//It is called when the button is
clicked
function fun(){
var myObj,
x;
//myObj is the
json array which stores message as key
//and Hello
World as its value
myObj = {
"message":"Hello World"
};
//x is a variable that store the
value of the key message from myObj json array
x = myObj['message'];
//The below statement find the
element with the id="demo"
//and replaces its contents with
that of the one store in the variable x
document.getElementById("demo").innerHTML = x;
}
</script>
</head>
<body>
<!--This button calls the function fun() when
clicked -->
<button onclick="fun()">Click
Me</button>
<!--The below is a heading tag with id="demo" it is
used to print the message from json array -->
<h1 id="demo"></h1>
</body>
</html>
Code in the image:
Output: