3

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 ?

7
  • Where do you actually call the function? Commented Sep 20, 2012 at 17:28
  • I see no place where you specify hello as onclick event handler Commented Sep 20, 2012 at 17:30
  • Seems to work here jsfiddle.net/j08691/k9g5e Commented Sep 20, 2012 at 17:30
  • calling hello() or add(), it doesn't work... Commented Sep 20, 2012 at 17:33
  • Both work fine in @j08691's example. Commented Sep 20, 2012 at 17:35

2 Answers 2

3

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

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

1 Comment

Damn ! it did not appeared on my editor... Thanks it fixed it!
1

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.

1 Comment

Thanks, yeah I wasn't at this part yet, but still helpful

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.