+1 vote
80 views
by (98.9k points)
Q3 A Explain the Document Object Model using a diagram. Write a code in JavaScript for
any one of the following:
1) To change the background color of the web page automatically after every 5
seconds.
2) To display three radio buttons on the web page, namely, "Red", "Blue" and
"Green". Selecting any button changes the background color as per the name of the
button.

1 Answer

0 votes
by (98.9k points)
 
Best answer

The Document Object Model (DOM) is a programming interface for web documents. It represents the structure of a document as a tree of objects, where each object corresponds to a part of the document, such as elements, attributes, and text. The DOM is used by browsers to render web pages and allows scripts to dynamically update the content, structure, and style of a document.

Document Object Model - Wikipedia

 

Write a code in JavaScript for any one of the following:
1) To change the background color of the web page automatically after every 5 seconds.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Background Color Change</title>
  <script>
    function changeBackgroundColor() {
      const colors = ['red', 'blue', 'green'];
      const randomColor = colors[Math.floor(Math.random() * colors.length)];
      document.body.style.backgroundColor = randomColor;
    }

    setInterval(changeBackgroundColor, 5000);
  </script>
</head>
<body>
  <h1>Automatic Background Color Change</h1>
</body>
</html>


2) To display three radio buttons on the web page, namely, "Red", "Blue" and "Green". Selecting any button changes the background color as per the name of the button.

 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Change Background Color</title>
</head>
<body>

<form>
  <label>
    <input type="radio" name="color" value="red" onclick="changeBackgroundColor(this.value)">
    Red
  </label>
  <label>
    <input type="radio" name="color" value="blue" onclick="changeBackgroundColor(this.value)">
    Blue
  </label>
  <label>
    <input type="radio" name="color" value="green" onclick="changeBackgroundColor(this.value)">
    Green
  </label>
</form>

<script>
  function changeBackgroundColor(color) {
    document.body.style.backgroundColor = color;
  }
</script>

</body>
</html>

 

 

Related questions

+1 vote
1 answer 49 views
+1 vote
1 answer 103 views
+1 vote
1 answer 179 views

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

504 users

...