Okay, I've spent hours trying this, and also checking out the search engines, and I've hit a brick wall...hoping someone can help me with this.
What I have is a drop down. Based on the selection in that drop down using Javascript, I want to produce either a text box, or a second drop down. I want to populate that second drop down with some options. It is populated using AJAX with the valid values pulled from a MySQL database.
This all works great.
However, my problem is that if the option is changed after initial selection it will still work, unless the drop down is generated. At that point, changing the option in the first drop down will never change the generated drop down to a text box. I always get an object is required javascript error. I have tried destroying the object, deleting, unsetting, etc...but for the life of me, can't come up with anything to get it working.
Here is my initial drop down with the javascript call:
Code:
<select name="sel" id="sel" onChange="toggleField(this.value);">
<option value="sales_rep">Sales Rep</option>
<option value="customer">Customer Name</option>
<option value="year">Trade Year</option>
<option value="vin">Vin Number</option>
<option value="wonlost">Status</option>
</select>
and here is the javascript function (toggleField). I know it's sloppy, only because I was manipulating it trying to make it work. I would make it much cleaner in production.
Code:
function toggleField(val) {
var year = document.getElementById('years');
var wonlost = document.getElementById('winlost');
var sales = document.getElementById('sales_rep');
var cust = document.getElementById('customer');
var vin= document.getElementById('vinno');
year.style.display = 'none';
wonlost.style.display = 'none';
sales.style.display = 'none';
cust.style.display = 'none';
vin.style.display = 'none';
if(val == 'year'){
year.style.display = 'inline';
}
else{
year.style.display = 'none';
}
if(val == 'wonlost'){
wonlost.style.display = 'inline';
}
else{
wonlost.style.display = 'none';
}
if(val == 'sales_rep'){
sales.style.display = 'inline';
}
else{
sales.style.display = 'none';
}
if(val == 'customer'){
cust.style.display = 'inline';
}
else{
cust.style.display = 'none';
}
if(val == 'vin'){
vin.style.display = 'inline';
}
else{
vin.style.display = 'none';
}
}
This piece of code is the fields showing or hiding when the first drop down is selected.
Code:
<div id="optdiv" style="display:inline;">
<input type="text" name="sales_rep" id="sales_rep" value="Sales" style="display: none;">
<input type="text" name="customer" id="customer" value="Customer" style="display: none;">
<select name="years" id="years" onmouseover="getOpt('findcity.php?type=year');" style="display: none;">
<option>Select Year</option>
</select>
<input type="text" name="vinno" id="vinno" value="Vin" style="display: none;">
<select name="winlost" id="winlost" onmouseover="getOpt('findcity.php?type=wonlost');" style="display: none;">
<option>Select Status</option>
</select>
</div>
The div is used to assign the location of the dynamically populated data, loaded from MySQL through AJAX, which, as I mentioned, works great.
As a final note, using AJAX, etc, I'm hoping not to post back until the selection is made, my button is clicked, and the results are pulled from the database.
Thank you with any help you can provide...I'm about to pull out the little hair that's left!
rob
What I have is a drop down. Based on the selection in that drop down using Javascript, I want to produce either a text box, or a second drop down. I want to populate that second drop down with some options. It is populated using AJAX with the valid values pulled from a MySQL database.
This all works great.
However, my problem is that if the option is changed after initial selection it will still work, unless the drop down is generated. At that point, changing the option in the first drop down will never change the generated drop down to a text box. I always get an object is required javascript error. I have tried destroying the object, deleting, unsetting, etc...but for the life of me, can't come up with anything to get it working.
Here is my initial drop down with the javascript call:
Code:
<select name="sel" id="sel" onChange="toggleField(this.value);">
<option value="sales_rep">Sales Rep</option>
<option value="customer">Customer Name</option>
<option value="year">Trade Year</option>
<option value="vin">Vin Number</option>
<option value="wonlost">Status</option>
</select>
and here is the javascript function (toggleField). I know it's sloppy, only because I was manipulating it trying to make it work. I would make it much cleaner in production.
Code:
function toggleField(val) {
var year = document.getElementById('years');
var wonlost = document.getElementById('winlost');
var sales = document.getElementById('sales_rep');
var cust = document.getElementById('customer');
var vin= document.getElementById('vinno');
year.style.display = 'none';
wonlost.style.display = 'none';
sales.style.display = 'none';
cust.style.display = 'none';
vin.style.display = 'none';
if(val == 'year'){
year.style.display = 'inline';
}
else{
year.style.display = 'none';
}
if(val == 'wonlost'){
wonlost.style.display = 'inline';
}
else{
wonlost.style.display = 'none';
}
if(val == 'sales_rep'){
sales.style.display = 'inline';
}
else{
sales.style.display = 'none';
}
if(val == 'customer'){
cust.style.display = 'inline';
}
else{
cust.style.display = 'none';
}
if(val == 'vin'){
vin.style.display = 'inline';
}
else{
vin.style.display = 'none';
}
}
This piece of code is the fields showing or hiding when the first drop down is selected.
Code:
<div id="optdiv" style="display:inline;">
<input type="text" name="sales_rep" id="sales_rep" value="Sales" style="display: none;">
<input type="text" name="customer" id="customer" value="Customer" style="display: none;">
<select name="years" id="years" onmouseover="getOpt('findcity.php?type=year');" style="display: none;">
<option>Select Year</option>
</select>
<input type="text" name="vinno" id="vinno" value="Vin" style="display: none;">
<select name="winlost" id="winlost" onmouseover="getOpt('findcity.php?type=wonlost');" style="display: none;">
<option>Select Status</option>
</select>
</div>
The div is used to assign the location of the dynamically populated data, loaded from MySQL through AJAX, which, as I mentioned, works great.
As a final note, using AJAX, etc, I'm hoping not to post back until the selection is made, my button is clicked, and the results are pulled from the database.
Thank you with any help you can provide...I'm about to pull out the little hair that's left!
rob