Results 1 to 3 of 3

Thread: DOM onclick event not working in form?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2001
    Location
    The Netherlands
    Posts
    94

    DOM onclick event not working in form?

    A simple thing but i can't get it work.
    What i want to achieve is just being able to get that message box when u click on the submit button in the form.
    When i use the script below and i load the html page i immediately get the popup box and when i click the button nothing happens .

    Can somebody help me out?

    Code:
    <html>
    
    <body>
    <form name="myForm">
    The form's name is: <input type="text" name="text1" size="20">
    <br /><br />
    <input type="button" value="Show the form's name">
    
    <script language="JavaScript" type="text/JavaScript">
    document.forms[0].elements[0].onclick=window.alert('You clicked');
    </script>
    
    </form>
    </body>
    </html>
    "Against All Odds"

  2. #2
    Addicted Member
    Join Date
    Jul 2004
    Location
    Mumbai
    Posts
    236

    Re: DOM onclick event not working in form?

    hi choller,

    just do this change and you'll make it.

    Code:
    <html>
    
    <body>
    <form name="myForm">
    The form's name is: <input type="text" name="text1" size="20">
    <br /><br />
    <input type="button" value="Show the form's name" onclick="show()">
    
    <script language="JavaScript" type="text/JavaScript">
    function show() {
    window.alert('You clicked');
    }</script>
    
    </form>
    </body>
    </html>
    --Kishore...

  3. #3
    Member
    Join Date
    Mar 2005
    Posts
    56

    Re: DOM onclick event not working in form?

    You were accessing the first index of the elements array, which would be the text box, but i'm assuming this is what you were after.
    Code:
    <html>
    
    <body>
    <form name="myForm">
    The form's name is: <input type="text" name="text1" size="20">
    <br /><br />
    <input type="button" value="Show the form's name">
    
    <script language="JavaScript" type="text/JavaScript">
    document.forms[0].elements[1].onclick = function()
    {
    	alert('You clicked');
    }
    </script>
    
    </form>
    </body>
    </html>
    kishore.kr's works just as well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width