SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)

    Form elements showing no properties.

    Got a slight problem and indeed a question as I want to understand WHY the following is happening.

    I have set up a small test script as I was having the problem with a much larger form but the problem is the same....

    The Script
    Code:
    <script type="text/javascript">
    
    function getElem(f) {
     	// alert(f); // returns the form name as expected
     	// alert(f1.elements["texttest"].value); // returns the element value as expected
    	alert(f.elements["texttest"].value); // f.elements has no properties....?
    }
    </script>
    <h2>Form 1</h2>
    <form name="f1" id="f1">
    <input type="text" name="texttest" id="texttest" >
    <input type="button" name="sendElem" onclick="getElem('f1');" />
    </form>
    
    <h2>Form 2</h2>
    <form name="f2" id="f2">
    <input type="text" name="texttest2" id="texttest2" >
    <input type="button" name="sendElem2" onclick="getElem('f2');" />
    </form>
    The Problem
    When the following script is called and asked to alert() the parameter (form name) it does.....
    When asked to alert the value of one form element it sends back
    Error: f.elements has no properties
    So what is going on? Why does it not pass the parameter of f as the form name so that it can be used later?

    Cheer

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're sending in a string argument to your function, trying to use a .elements property of a string constant.

    Try,
    Code:
    function getElem(f) {
     	var o = document.getElementById(f);
     	// alert(f); // returns the form name as expected
     	// alert(f1.elements["texttest"].value); // returns the element value as expected
    	alert(o.elements["texttest"].value); // f.elements has no properties....?
    }
    Birnam wood is come to Dunsinane

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    Many thanks Tommy, that works nicely.
    So a string cant be sent to the function as an argument.....?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it can, but it is just a string. A string doesn't have a .elements property. The string "f1" is not the same as the form whose name or ID is f1.

    You were passing a string argument to the function, but the function expected the argument to be a HTMLFormElement node.
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

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