0

Hi, I am new to javascript. Syntactically, this code seems correct, but for some strange reason, it refuses to execute. I know it's bad practice to include the script within the same document, but I just want this to work. Thank you in advance.

 <!DOCTYPE html>
 <html>
        <head>
            <title>
                Test
                </title>

                <body>
                    <p>
                        Hello World.
                        </p>
                        <br>
                        <p id = "bye">
                            </p>

                        <canvas id="canvas1" width="200" height = "100" style="border:1px solid #000000;"> 
                            </canvas>

                            <script>
                                var c=document.getElementById("myCanvas");
                                var ctx=c.getContext("2d");
                                ctx.fillStyle="#FF0000";
                                ctx.fillRect(0,0,150,75);

                                document.getElementById("bye").innerHTML = "bye";
                                </script> 
                    </body>
            </head>
    </html>
3
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by rayidi
1

Hi,
I think that you need to change
var c=document.getElementById("myCanvas");
Into
var c=document.getElementById("canvas1");
myCanvas for canvas1.
canvas1 is the id that you have done to it.

0

On your code you written entire body in head seaction. try to keep seperately.

<html>
    <head>
        <title>You'r document title here</title>
    </head>
    <script>
        // your script goes here
    </script>
    <body>
        <!-- Content goes here -->
        <script>
            // Your script goes here
        </script>
    </body>
</html>

to avoid errors.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.