0

<!DOCTYPE html>
    <html>
 <head>
  <title> Javascript 1 </title>
  <style type = "text/css">
   #firstid
   {
    color:red;
    background-color:blue;
    font-size:25px;
    font-style:italics;
   }
   #firstid:hover
   {
    color:white;
   }
   .firstclass
   {
    color:cyan;
    background-color:pink;
    font-size:35px;
    font-face:verdana;
   }
  </style>
  <script type = "text/javascript">
  document.write("<h1>Hello<br/></h1> ");
  function changeBG()
  {
   document.body.stlyle.background="green";
   var x = document.getElementById("firstid");
   x.style.color="brown";
   x.style.fontSize="10px";
  }
  
  </script>
 </head>
 <body>
  <pre id="firstid">
   To do:
   1. Class
   2. ID
   3. Function
  </pre><br/>
  <p class="firstclass">
   Harry Potter is a wizard.
  </p> 
  
  <button type="button" onClick="changeBG()">Change To do</button>
 </body>
    </html> 
 

This is the code.

I created a function changeBG() and a button "Change To do" . When I click the button, this function should run but it doesn't run. Please solve this problem.

The function includes changing background and changing color and fontsize of the specified id.

prasanth
  • 22,145
  • 4
  • 29
  • 53
Karan Nagpal
  • 15
  • 1
  • 1
  • 5

3 Answers3

2

you mispelled

document.body.stlyle.background="green";

to

document.body.style.background="green";
Beginner
  • 4,118
  • 3
  • 17
  • 26
0

u have wrong with typing style word
document.body.stlyle.background="green";

Ammar Mousa
  • 48
  • 10
0

Change to:

<!DOCTYPE html>
<html>
<head>
<title> Javascript 1 </title>
<style type = "text/css">
#firstid
{
color:red;
    background-color:blue;
    font-size:25px;
    font-style:italics;
}
#firstid:hover
{
color:white;
}
.firstclass
{
color:cyan;
    background-color:pink;
    font-size:35px;
    font-face:verdana;
}
</style>

<script type = "text/javascript">
document.write("<h1>Hello<br/></h1> ");

</script>
</head>
<body>
<pre id="firstid">
To do:
1. Class
2. ID
3. Function
</pre><br/>
<p class="firstclass">
Harry Potter is a wizard.
</p>

<button type="button" onClick="changeBG()">Change To do</button>
</body>


<script type = "text/javascript">

function changeBG()
{

    document.body.style.background="green";
    var x = document.getElementById("firstid");
    x.style.color="brown";
    x.style.fontSize="10px";
}

</script>

</html>
Vikram Biwal
  • 2,618
  • 1
  • 25
  • 36