HTML Form onclick event doesn't call javascript function

12,319

Solution 1

Looks like it have some kind of special character before:

 </script>.

I just copied and pasted Here. Look the first character in the 13th line

Solution 2

I copied this into a nice little jsfiddle here http://jsfiddle.net/dr9hG/

I also changed the name from just "email" to "email[]" so that you can access all of them by array on the page this submits to.

Share:
12,319
TheSquad
Author by

TheSquad

Updated on June 05, 2022

Comments

  • TheSquad
    TheSquad almost 2 years

    I would like to create a dynamic form, adding inputs into the form by pressing a button (or an image) when I put the code directly on the onclick event

    alert("Hello");
    

    it works... however, it doesn't when I try to call a javascript function... Even If I call the simple function hello(); (as well with add() )

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script type="text/javascript">
    function add() {
        var container = document.createElement("div"); 
        container.innerHTML = "<input type='text' name='email'><br/>";
        document.getElementById('emails').appendChild(container);
    }
    function hello() {
        alert("Hello"); 
    }
    ​</script>
    </head>
    <body>
    <form id="myform" name="myform" action="valid.php" method="POST">
        <div id="emails">
            <input type='text' name='email' value=''/><br />
        </div>
        <img width="50px" src="myimage.jpg" onclick='add();'/>
        <input type="submit" value="Submit"/>
    </form>
    </body>
    </html>
    

    Anyone manage to spot why ?

  • TheSquad
    TheSquad over 11 years
    Damn ! it did not appeared on my editor... Thanks it fixed it!
  • TheSquad
    TheSquad over 11 years
    Thanks, yeah I wasn't at this part yet, but still helpful