Click to See Complete Forum and Search --> : passing a value to a hidden form field


trojan_uk
12-10-2003, 05:35 AM
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;
}

AdamBrill
12-10-2003, 07:08 AM
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]);

trojan_uk
12-10-2003, 07:47 AM
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

AdamBrill
12-10-2003, 08:16 AM
Take a look at this(an example of changing the form field value):<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.

trojan_uk
12-10-2003, 09:52 AM
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

AdamBrill
12-10-2003, 10:37 AM
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. :)