JAVASCRIPT GETELEMENT METHOD

Lavanya



JavaScript document.getElement Method 

The `document.getElementById()` method retrieves the element with the specified ID.

In the previous page, we used `document.form1.name.value` to get the value of the input field. Alternatively, we can use the `document.getElementById()` method to retrieve the input value, but we must first define an ID for the input field.

Example 


Here is a simple example of the `document.getElementById()` method that prints the cube of a given number.

<Script>

function cubeNumber() {
  var num = document.getElementById("number").value;
  num = parseInt(num);
  var cube = num * num * num;
  document.getElementById("result").innerHTML = "Cube of " + num + " is: " + cube;
}

</Script>

Output:

Enter a number: 5
Cube of 5 is: 125


Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send