# onclick in html - javascript not being called



## ilugd (Aug 23, 2008)

i have this webpage in which i am trying to dynamically add a text input box. I haven't got as far as to see if what i am trying to do is happening. I am still stuck at getting my click button to add the input box. I don't know what I am doing wrong. 

The addtextbox function is supposed to add the element.

On clicking on the button Click, the function is supposed to be called. I added the alert call to check if the function is called.

```
<html>
    
  <head>
    <title></title>
  </head>
  <body>
      <script language="javascript">
		function addtextbox(){
		alert("ok, function called");
		var frm=document.forms[0];
		var newtxt=document.createElement('input');
		newtxt.setAttribute('type','text');
		newtxt.setAttribute('value','new box');
		newtxt.setAttribute('value','formvalue[]');
		frm.appendChild(newtxt);
}		
		;   
   </script>
    <form action="index.php" method="get">
    <input name="formvalue[]" type=text value="default value"/>

    <input type="button" value="Click to add box" onclick()="addtextbox();"/>
    <input type="submit" />
    
        
        
</form>
  </body>
</html>
```


----------



## RCuber (Aug 23, 2008)

remove the brackets from onclick()
Your Code

```
<input type="button" value="Click to add box" onclick()="addtextbox();"/>
```

Change to

```
<input type="button" value="Click to add box" onclick="addtextbox();"/>
```


----------



## ilugd (Aug 23, 2008)

@charan. Thanks a lot. It works now. Guess too much of learning java made me confused with how html works.


----------



## mehulved (Aug 23, 2008)

it's onclick on onclick()
use vim/emacs, makes life easy, always.


----------



## RCuber (Aug 23, 2008)

ilugd said:


> @charan. Thanks a lot. It works now. Guess too much of learning java made me confused with how html works.


welcome


----------

