CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2002
    Location
    Mumbai
    Posts
    98

    Smile HTML form Control event handling

    Hi Can anybody help me to correct this one?

    I am getting error message "document.form.Test2" is null or not an object.

    I am new to HTML and Scripting.


    <html>
    <title>JAVASCRIPT</title>

    <script language = "JavaScript" type="text/javascript">

    function CalculateTotalImpact(){

    alert( document.form.Test2.value);

    }
    </script>

    <BODY>

    <input id="Test1" type="text" value = "" onchange = "CalculateTotalImpact()" />
    <input id="Test2" type="text" value = "" onchange = "CalculateTotalImpact()" />
    <INPUT TYPE="button" ONCLICK="CalculateTotalImpact()" VALUE="click to say hello">

    </BODY>
    </HTML>

    Thank you.

    Amit

  2. #2
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: HTML form Control event handling

    You need a form tag around your input fields for them to be considered a form.
    http://www.w3.org/TR/html4/interact/forms.html

    <form name="myForm" method="post" action="www.someplace.com">
    //your input fields here
    </form>

    then you can access the fields:
    document.myForm.myInputField

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: HTML form Control event handling

    Yes Alsvha is correct as usual!
    You could try something like:
    PHP Code:
    <HTML>
    <
    HEAD>
    <
    TITLE>JAVASCRIPT</TITLE>
    </
    HEAD>

    <
    script language "JavaScript">

    function 
    CalculateTotalImpact(){

    alertdocument.form.Test2.value); 

    }
    </
    script>

    <
    BODY>
    <
    FORM>
    <
    input id="Test1" type="text" value "" onchange "CalculateTotalImpact()" >
    <
    input id="Test2" type="text" value "" onchange "CalculateTotalImpact()" />
    <
    INPUT TYPE="button" ONCLICK="CalculateTotalImpact()" VALUE="click to say hello"
    </
    FORM>
    </
    BODY>
    </
    HTML
    Give that a try.
    I just have a couple of issues with your code.

    The Head tag- I'm old fashioned, but it makes for better proper reading when included

    You don't have to say:
    PHP Code:
    <script language "JavaScript" type="text/javascript"
    You could use either
    PHP Code:
    <script language "JavaScript"
    or
    PHP Code:
    <script type="text/javascript"
    As far as I know the language attribute will eventually be replaced with : type="text/javascript", so it's actaully better to use it instead of language.

    PHP Code:
    <input id="Test1" type="text" value "" onchange "CalculateTotalImpact()" /> 
    I may be wrong, but the Input tag is not a container tag, meaning that it doesn't have a closing tag. You don't need to include the "/" in there it just needs to be:
    PHP Code:
    <input id="Test1" type="text" value "" onchange "CalculateTotalImpact()" 

    I hope these comments were useful

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

Featured