lundi 22 mai 2017

Why is this code working and not the other one? JavaScript

<!DOCTYPE HTML>
<html>
<head>
    <title></title>
</head>
<body>
        <input name="usrname" type="text" placeholder="Username" />
        <input name="comnts" type="text" placeholder="comments"/>
        <input id="btnButton" type="Submit" value="Click me"/>
</body>
 <script type="text/javascript">
        window.onload = function(){
           var refButton = document.getElementById("btnButton");
            refButton.onclick = function() {
                window.alert("Hi");
                //var name-"Bhuvanesh";
                //var Comment="Zack";
                //window.alert("Hello");
            }
        };
    </script>
</html>

This code above works and the code below does not work

    <!DOCTYPE HTML>
<html>
<head>
    <title></title>
</head>
<body>
        <input name="usrname" type="text" placeholder="Username" />
        <input name="comnts" type="text" placeholder="comments"/>
        <input id="btnButton" type="Submit" value="Click me"/>
</body>
 <script type="text/javascript">
        window.onload = function(){
           var refButton = document.getElementById("btnButton");
            refButton.onclick = function() {
                window.alert("Hi");
                var name-"Bhuvanesh";
                var Comment="Zack";
                window.alert("Hello");
            }
        };
    </script>
</html>

I do not understand why the code below does not work. I have used window.onload to make sure the page renders first. Somehow the code does not seem to work. I am trying to make a simple page that can accept Name and comment from the textbox and display it on the webpage. I am relatively new to web development. Any help is appreciated.




Aucun commentaire:

Enregistrer un commentaire