Example:
<html lang="en">
<head>
<title>getElementById example</title>
</head>
<body>
<p id="para">Some text here</p>
<button onclick="changeColor('blue');">blue</button>
<button onclick="changeColor('red');">red</button>
</body>
<script type="text/javascript" language="javascript">
function changeColor(newColor) {
// the method to represent the element whose id property attribute is "para".
// elem const as "para" Id representation.
const elem = document.getElementById("para");
// to change text color of paragraph element that id attribute is "para".
elem.style.color = newColor;
}
</script>
</html>
The above example is used to change text color of paragraph element that id attribute is "para".
The getElementById() method is only available as a method of the global document object, and not available as a method on all element objects in the DOM.
Tidak ada komentar:
Posting Komentar