Monday, 17 July 2023

getElementById in JavaScript_3

 
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>
<head>
  <title>getElementById example</title>
  <script>
  function changeColor(newColor) 
  {
    var elem = document.getElementById("para1");
    elem.style.color = newColor;
  }
  </script>
</head>
<body>
  <h1 id="para1">Some text here</h1>
  <button onClick="changeColor('blue');">blue</button>
  <button onClick="changeColor('red');">red</button>
   <button onClick="changeColor('green');">green</button>
    <button onClick="changeColor('yellow');">yellow</button>
     <button onClick="changeColor('pink');">pink</button>
</body>
</html>

0 comments

Post a Comment