onclick in html - javascript not being called

Status
Not open for further replies.

ilugd

Beware of the innocent
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.
Code:
<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

The Mighty Unkel!!!
Staff member
remove the brackets from onclick()
Your Code
Code:
<input type="button" value="Click to add box" onclick()="addtextbox();"/>

Change to
Code:
<input type="button" value="Click to add box" onclick="addtextbox();"/>
 
OP
ilugd

ilugd

Beware of the innocent
@charan. Thanks a lot. It works now. Guess too much of learning java made me confused with how html works.
 
Status
Not open for further replies.
Top Bottom