www.webdeveloper.com
Results 1 to 6 of 6

Thread: passing a value to a hidden form field

  1. #1
    Join Date
    Dec 2003
    Posts
    6

    passing a value to a hidden form field

    Hi,

    I use the following script below to exstract a value from an array and display it on the page depending on what value was selected from a drop down menu, what I would also like to do is then also add the value to a hidden form field but I am not sure how I can get the value into the field. the section in red is how I thought I might do it but it doesn't work. JS is not my strong point so any help would be great

    Many thanks
    Peter


    var NODE = <%=arrNode%>

    function showResult(sel){
    document.getElementById("disp").innerHTML = "&nbsp;" + NODE.find([sel.options[sel.selectedIndex].value]);
    document.form1.cil_name.value = NODE.find([sel.options[sel.selectedIndex].value]);
    }


    //this creates a new array function for the arrNode Array
    NODE.find = function(what){
    var i=0;
    for (;this[i]!=what && i<this.length-2;i+=2){}
    if (this[i]==what) return this[i+1]; else return false;
    }

  2. #2
    Join Date
    Dec 2002
    Location
    WI, United States
    Posts
    1,372
    It looks like it is correct. Try changing the form field to be type="text" so that you can see if it is putting the value in there. If it is not, are you sure that NODE.find([sel.options[sel.selectedIndex].value]); returns a value? Try changing that line to:

    document.form1.cil_name.value = "It worked!";

    and see if it will work to do that. If so, then there must be something wrong with NODE.find([sel.options[sel.selectedIndex].value]);

  3. #3
    Join Date
    Dec 2003
    Posts
    6
    The code does work because the section of code below does produce the correct result and prints it within the page using <div id="disp"></div>

    document.getElementById("disp").innerHTML = "&nbsp;" + NODE.find([sel.options[sel.selectedIndex].value]);

    but what I am not sure how to do is to get the result into the value of a hidden for field.

    Thanks again

  4. #4
    Join Date
    Dec 2002
    Location
    WI, United States
    Posts
    1,372
    Take a look at this(an example of changing the form field value):
    Code:
    <html>
    <head>
    	<title>Changing the value of a form field</title>
    <script type="text/javascript">
    function run_func(){
    	document.form1.cil_name.value="This is a test.";
    	//and make sure it put it in there:
    	alert(document.form1.cil_name.value);
    	//this will alert "This is a test."
    }
    </script>
    </head>
    
    <body>
    
    <form name="form1" action="wherever.php" method="post">
    <input type="hidden" name="cil_name">
    <input type="button" value="Add Text To The Hidden Field" onclick="run_func();">
    </form>
    
    </body>
    </html>
    You should be able to see how it works from that.

  5. #5
    Join Date
    Dec 2003
    Posts
    6
    Its ok I changed the line to:

    document.getElementById("cil_name").value = NODE.find([sel.options[sel.selectedIndex].value]);

    I then did a response.write to check if a value had been passed and it was, so I guess that is sorted now.

    Many thanks for taking the time to help
    Peter

  6. #6
    Join Date
    Dec 2002
    Location
    WI, United States
    Posts
    1,372
    I could be wrong, but check if you named the form "form1." I have a feeling that was probably the problem with what you had, since if the form would be named that, it should have worked fine. I'm glad you got it working, though.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles