getElementById
- The getElementById() method returns an element with a specified value.
- The getElementById() method returns null if the element does not exist.
- The getElementById() method is one of the most common methods in the HTML DOM. It is used almost every time you want to read or edit an HTML element.
- If two or more elements with the same id exist, getElementById() returns the first.
Example:
<!DOCTYPE html>
<html>
<body>
<h1 id="demo">Click the button to change the color of this paragraph.</h1>
<h2 id="demo1">Click the button to change the color of this paragraph.</h2>
<button onClick="myFunction()">Try it</button>
<script>
function myFunction()
{
var x = document.getElementById("demo");
x.style.color = "red";
var y = document.getElementById("demo1");
y.style.color = "green";
}
</script>
</body>
</html>
0 comments
Post a Comment