PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 16, 2022

[FIXED] Why is only the last output statement executed?

 August 16, 2022     javascript, output     No comments   

Issue

I'm new to Javascript and I've tried to print multiple Strings but only the last statement is executed, Code:

<html>
<body>
    <p id = "demo"></p>
    <script>
        document.getElementById("demo").innerHTML = "Toit"
        document.getElementById("demo").innerHTML = "Noice";
        document.getElementById("demo").innerHTML = "Epic"; 
    </script>
</body>
Output:
Epic

Solution

Every statement override the previos one.

You should do:

<html>
<body>
    <p id = "demo"></p>
    <script>
        document.getElementById("demo").innerHTML = "Toit"
        document.getElementById("demo").innerHTML += "Noice";
        document.getElementById("demo").innerHTML += "Epic"; 
    </script>
</body>

By using += you concat the last value, which is exactly the same like doing:

document.getElementById("demo").innerHTML = 
  document.getElementById("demo").innerHTML + "Epic"; 


Answered By - guyaloni
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing