Javascript getElementByID and Form Handling

Go To StackoverFlow.com

1

I'm currently having some trouble with getting certain input fields passed along when a dynamic form is submitted.

The city/school fields are enabled if a certain field in the state is selected (because we only need to collect info from students from a particular state and not others).

HTML code:

<form name="email" method="post" action="...email.php">
. 
. 
. 
<div id="cityDiv" class="row">
<h5 id="c1" class="mandatory">City:</h5>
<select id="c2" class="inputColumn" onchange="fillSchoolDropdown();" name="extra_city">
  --select options ---  
 </div>
 <div id="schoolDiv" class="row">
 <h5 id="sch1" class="mandatory">School:</h5>
 <input id="extra_school" class="inputColumn" type="text" name="extra_school">
 </div>

Javascript :

   if(verify(document.email)){ 
     .
     .
     .
   var school = document.getElementById("extra_school").value;
   var citySelect=document.getElementById("extra_city");
   var city=citySelect.options[citySelect.selectedIndex].text;

I've been either getting null CitySelect or the form successfully validates but somehow...the end result for the two are still " " despite what is entered in the form.

What could I be doing wrong here?

2012-04-04 21:46
by Andy Chen
What's CountySelect? Could you make your HTML and script snippets match and include everything relevant - Andrew Leach 2012-04-04 21:49
corrected that. Sorry - Andy Chen 2012-04-04 21:52


1

var citySelect=document.getElementById("extra_city");

Should be:

var citySelect=document.getElementById("c2");

Because that's the id. You are trying to select the name attr.

Or you could change the id of the select element:

<select id="extra_city" class="inputColumn" onchange="fillSchoolDropdown();" name="extra_city">
2012-04-04 21:50
by PeeHaa
Would this still work if this city dropdown was loaded in later by Javascript/AJAX and not loaded initially - Andy Chen 2012-04-04 21:55
What about just hiding it initially. And load the options using AJAX and then showing it - PeeHaa 2012-04-04 22:00
As far as I know, there's a div for it and the fillCityDropdown() loads the City Dropdown and once a city is selected, an input box basically fills in another div.

Both divs are empty when the page loads.

Somehow this wasn't a problem before when it worked like this, but I'm required to make some changes to what the form outputs and I can't imagine it being too much different from what it currently is - Andy Chen 2012-04-04 22:51



0

Your select element has the id of 'c2', but you are trying to call by its name 'extra_city'

var citySelect=document.getElementById("c2");
2012-04-04 21:50
by Juan Mendes
Ads