Link to home
Start Free TrialLog in
Avatar of dyarosh
dyarosh

asked on

Can't access my hidden form field in my javascript

I have a form that when submitted, I want to set the hidden fields using javascript.  The fields are going to contain the text from drop down menus.  I can't set the value of the drop downs to the text because I need different information for the value.

I've included the form definition and the code from the Javascript function.  In the Javascript function, the first alert shows me the text (Project Name 1), the second alert shows me the text (Program 1), and the third alert shows me the value in the input text field (some text here).  The last alert is never shown.  I know the hidden fields are defined because when the form is posted, I am able to pull the value from them.  They just don't contain the value I want (contain the initial value).

If anyone can point out where my mistake is I would appreciate it.
Form definition
-----------------------------------------------------------------------
<form name="ProjectsForm" id="ProjectsForm" method="post" action="">
<input type="hidden" id="projectname" name="projectname" value="Error project name">
<input type="hidden" id="programname" name="programname" value="Error program name">
<select name="Projects" id="Projects">
<option>Select Project Survey</option>
<option value="1">Project Name 1</option>
</select>
<input type="text" id="test" name="test" value="some text here"></input>
<select name="surveys" id="surveys">
<option>Select Program</option>
<option value="34">Program 1</option>
</select>
<input type="text" id="test" name="test" value="some text here"></input>
<input type="submit"  onClick="setHiddenValues()" name="Build" value="Build Survey">
</form>
 
 
Javascript Function in HEAD Section
------------------------------------------------------------------------
function setHiddenValues()
{
	var w=document.getElementById("Projects").selectedIndex;
	var x=document.getElementById("Projects");
	alert(x.options[w].text);
	
	w=document.getElementById("surveys").selectedIndex;
	x=document.getElementById("surveys");
	alert(x.options[w].text);
	alert(document.getElementById("test").value);
	alert(document.getElementById("projectname").value);
	return true;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of bluV11t
bluV11t
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dyarosh
dyarosh

ASKER

I must have had a typo somewhere because when I copied your code everything worked fine.  Thanks for the help.