The Document Object - Accessing HTML
Javascript becomes really interesting when we start to perform actions on HTML elements. We can use properties and attributes attached to the document object to achieve this.
Consider the demo below:
We use
document.getElementById("name").valueto get the value of the input box with the id attributenameand assign it to a variablenameValueWe can then use
document.getElementById("demo").innerHTML = "Hello " + nameValue;to display a message within a<p>element with an id ofdemoNotice how the code block to perform the above operations is wrapped in
function showName(). This function is invoked (run) when the button is clicked. The button element is given the attributeonClick="showName()in order to achieve this.
<html>
<head>
<title>JavaScript Page</title>
<!--
This script in included in the head so we need to call it
-->
<script type="text/javascript">
//This is a function
function showName(){
// create a variable called name and assign it the value of the name input box
var nameValue = document.getElementById("name").value;
/* we can now print the name. The . allows us to append to a string */
document.getElementById("demo").innerHTML = "Hello " + nameValue;
} //end function
</script>
</head>
<body>
<p> <input type="text" placeholder="enter name" id="name"> </p>
<!--
Notice the onClick attribute. This will call the function showName()
-->
<p> <button onClick="showName()">Click Me</button> </p>
<!--
We will display our output below
-->
<p id="demo"></p>
</body>
</html>