Answer to Question #316144 in HTML/JavaScript Web Application for siva

Question #316144

DOM Manipulations - 2

Use the below reference image.

Dynamically add the elements in HTML container element with id

myContainer to achieve the design.

Note

Use the checked property and classList.toggle method to achieve the functionality.

Resources

CSS Colors used:

#7a0ecc

#f2ebfe

HTML:

<body>

  <div id="myContainer"></div>

</body>

it should pass:

Page should consist of only one HTML checkbox input element with the HTML attribute id having a non-empty value.

Page should consist of only one HTML label element with the HTML attribute for having a non-empty value.

When the HTML checkbox input is clicked,the text content of the HTML label element should change and shouldn't be empty.

When the HTML checkbox input is clicked,the color of the HTML main heading element should change.


1
Expert's answer
2022-03-22T15:20:34-0400
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>DOM Manipulations - 2</title>
</head>
<body>
   <style>
      .purple{
         color: #7a0ecc;
      }
   </style>
   <div id="myContainer">
      <h1 id="title">Main title</h1>
      <input type="checkbox" id="change" name="change">
      <label for="change" id="change-label">Change</label>
   </div>
   <script>
      const checkbox = document.getElementById('change')
         const title = document.getElementById('title')
         const label = document.getElementById('change-label')


         function changeTextAndColor(event) {
            title.classList.toggle('purple')
            if (label.innerText === "Change") {
               label.innerText = "Change text!";
            } else {
               label.innerText = "Change";
            }
         }


         checkbox.addEventListener('change', changeTextAndColor)
   </script>
</body>
</html>

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS