Javascript Error : Is Null or Not an Object
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Javascript Error : Is Null or Not an Object

  1. #1

    Javascript Error : Is Null or Not an Object

    I have a javascript

    function sortThis(thisform, sortval)
    {
    thisform.sort.value = sortval;
    if(thisform.type.value == "ASC")
    thisform.type.value = "DESC";
    else
    thisform.type.value = "ASC";

    thisform.submit();
    }

    and 2 form elements and I have some hyper links where I say
    <FORM>
    <INPUT TYPE="hidden" Name="sort" VALUE="">
    <INPUT TYPE="hidden" Name="type" VALUE="">



    <A HREF="#" onClick="sortThis(this.form, 'PERSONS.last_name, PERSONS.first_name')">Name</A>
    </FORM>
    when I click this hyperlink I get a error message that sort is null or not an object! is there something wrong I am doing? When I use a button, this works fine but when done through a hyper link its not working. Any ideas?

  2. #2
    Join Date
    May 2004
    Location
    MN / NJ, United States
    Posts
    768

    Re: Javascript Error : Is Null or Not an Object

    The reason is because of the types of tags you are using. A button has a form property that references the form object, .form. Contrarily, a hyperlink doesn't have this property. You could use this.parentNode instead, which refers to the node one level up.
    *9-11-01* Never Forget; Never Forgive; Never Relent!
    "If we ever forget that we're one nation under God, then we will be a nation gone under." - Ronald Reagan

  3. #3

    Re: Javascript Error : Is Null or Not an Object

    and how do I use the this.parentNode ?

  4. #4
    Join Date
    Jun 2005
    Posts
    1,255

    Re: Javascript Error : Is Null or Not an Object

    The two following changes are needed:
    Code:
    ...
    <FORM name="my_form">
    ...
    <A HREF="#" onClick="sortThis(document.my_form, 'PERSONS.last_name, PERSONS.first_name')">Name</A>

  5. #5
    Join Date
    May 2004
    Location
    MN / NJ, United States
    Posts
    768

    Re: Javascript Error : Is Null or Not an Object

    This solution will work ... but if you move it to a different form, it still references the same form. To use .parentNode, use this example to see its use:
    Code:
    <form name="myForm">
      <a href=#" onclick="javascript:alert(this.parentNode.name)">Test</a>
      <!-- alerts myForm onclick -->
    </form>
    So really, replace this.form as your first argument with this.parentNode, and voila! Unless of course, you have the <a> inside a fieldset or div or something, but that isn't shown in your code.
    *9-11-01* Never Forget; Never Forgive; Never Relent!
    "If we ever forget that we're one nation under God, then we will be a nation gone under." - Ronald Reagan

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center