DIGIT-ARC read-only archive · snapshot: July 23, 2026

onclick in html - javascript not being called

5 posts · originally from https://geek.digit.in/community/threads/onclick-in-html-javascript-not-being-called.96227/

ilugd
ilugd Original post #1
Aug 23, 2008 at 1:23 AM
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
RCuber #2
Aug 23, 2008 at 2:38 AM
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();"/>
ilugd
ilugd #3
Aug 23, 2008 at 2:46 AM
@charan. Thanks a lot. It works now. Guess too much of learning java made me confused with how html works.
M
Aug 23, 2008 at 2:47 AM
it's onclick on onclick()
use vim/emacs, makes life easy, always.
RCuber
RCuber #5
Aug 23, 2008 at 2:51 AM
@charan. Thanks a lot. It works now. Guess too much of learning java made me confused with how html works.
welcome :)