0

This is the code of my html file:

<html>
    <head>
        <script type="text/javascript" src="try1.js"></script>
    </head>

    <body>

        <h1>A Web Page</h1>
        <p id="demo">A Paragraph</p>
        <button type="button" onclick="myFunction()">Try it</button>

    </body>
</html>

And this is the code of my javascript file:

function myFunction() {
     document.getElementById("demo").innerHTML = "Paragraph changed";
}
5
  • 1
    what do you mean it does not load.Did you check chrome developer tools and network tab of it.Did you find this js in that tab? Commented Jun 14, 2017 at 19:53
  • 1
    Is the try1.js file on the same folder with your HTML file? Commented Jun 14, 2017 at 19:56
  • Do not put parenthesis in onclick. it should be onclick="myFunction" Commented Jun 14, 2017 at 19:57
  • @Bill The above code works for me. Commented Jun 14, 2017 at 19:58
  • Hate to nitpick, but just a note: in general if you're only going to change the text of the element you should use innerText instead of innerHTML--it's a lot safer :) Commented Jun 14, 2017 at 20:07

2 Answers 2

4

Because the element doesn't exist yet. Try putting the javascript at the end of the body

<html>
    <head>
        <title>Example<title>
    </head>
    <body>

        <h1>A Web Page</h1>
        <p id="demo">A Paragraph</p>
        <button type="button" onclick="myFunction()">Try it</button>
        <script type="text/javascript" src="try1.js"></script>
    </body>
</html>

It seems to work just fine when inlining.

   
        <h1>A Web Page</h1>
        <p id="demo">A Paragraph</p>
        <button type="button" onclick="myFunction()">Try it</button>
        <script type="text/javascript">
        function myFunction() {
            document.getElementById("demo").innerHTML = "Paragraph changed";
        }
        </script>

Sign up to request clarification or add additional context in comments.

4 Comments

The issue is not that. The code document.getElementById("demo") will run when the button will be clicked, and at that time the element will exist on DOM
@TasosK. exactly what I meant.
@TasosK. I see. Well I inlined the code and it works fine. So maybe your js file is not in the same directory as the html file.
@styfle It works with his code as well.The problem here is probably his JS file name is either incorrect or path is wrong.
-1

The usual way to do this is to wrap your javascript in jquery's document ready function like this. If you're not using jquery, you should check it out, it's really useful.

$( document ).ready(function() {
    console.log( "ready!" );
});

3 Comments

Why are you introducing another library here.Plain JS does the job.
There is nothing in this question that necessitates the use of jQuery
Not trying to start a holy war, but if you're not using jquery in this day and age, you really should be. If you don't, you have to worry about things like cross-browser compatibility and you end up reinventing the wheel.

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.