0

I am new to JavaScript and am doing a project for a class. I want to create an Easter egg somewhere on the page that when clicked changes the whole page code to a single image. This is my code just to test out the concept:

<div id = "all">
    <p>The button below changes the whole page.</p>

    <button type = "button" onclick = "changePage()">Click Here</button>
</div>

<script>
    function changePage()
    {
        document.getElementById("all").innerHTML = "<img src = "stop.jpg" />";
    }
</script>

The content isn't replaced by the image stop.jpg, however when I change the line to:

document.getElementById("all").innerHTML = "<h1>Whatever</h1>";

The code changes to the simple heading with no problems. Any idea why?

0

2 Answers 2

1

Didn't you notice that quotes are repeating? You should wrap your img with another quotes:

'<img src = "stop.jpg" />'

Or escape them

"<img src = \"stop.jpg\" />"
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is with your quoting. The double quotes around the stop.jpg URL are ending the string that begins with <img. You need to use single quotes or escape the quotes:

document.getElementById("all").innerHTML = "<img src = 'stop.jpg' />";

document.getElementById("all").innerHTML = "<img src = \"stop.jpg\" />";

Didn't you get a syntax error in the Javascript console? Or notice the changed color in SO's syntax highlighting?

4 Comments

Oh OK. Wow I feel uneducated now. I don't know why I didn't think of that. Thanks, works now! :D
Haha sorry, just commented on the first one I read. Thanks to you both! And no I didn't get a syntax error, I'm simply using Notepad++ to create/edit my files.
You should have gotten a syntax error when you tried to load your page. Don't you check the Javascript console in the browser when you're trying to debug Javascript?
No, I don't know about the javascript console. When I said I'm new, I meant like just started using JavaScript this morning. lol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.