Skip to content
Advertisement

Show the content of the function in the div

How do I show myFunction content in myDiv div? So show “Example”?

function myFunction() {
  document.getElementById("demo").innerHTML = "Example!";
}
.myDiv {
  border: 5px outset red;
  background-color: lightblue;    
  text-align: center;
}
<div class="myDiv">
<p id="demo"></p>
</div>

Advertisement

Answer

This code:

<div class="myDiv">
<p> id="demo"</p>
</div>

Should be this:

<div class="myDiv">
<p id="demo"></p>
</div>

With the id tag inside <p>

And this:

function myFunction() {
  document.getElementById("demo").innerHTML = "Example!";
}

Should also get called since it is a function and not just the document.getElementById("demo").innerHTML = "Example!";

Example 1:

function myFunction() {
      document.getElementById("demo").innerHTML = "Example!";
    }
myFunction();

function myFunction() {
      document.getElementById("demo").innerHTML = "Example!";
    }
    
myFunction();
.myDiv {
  border: 5px outset red;
  background-color: lightblue;    
  text-align: center;
}
<div class="myDiv">
<p id="demo"></p>
</div>

Example 2:

document.getElementById("demo").innerHTML = "Example!";

document.getElementById("demo").innerHTML = "Example!";
.myDiv {
  border: 5px outset red;
  background-color: lightblue;    
  text-align: center;
}
<div class="myDiv">
<p id="demo"></p>
</div>
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement