![]() |
Getting Drop downs to "reset"
I am using Javascript so that when someone selects their state in a drop-down another one appears where they can select their city. Then once they do that information appears about sales reps in their area. This is working great except that if someone selects a state and city then decides to pick a different state the old information stays and nothing happens. I hope I'm explaining this clearly, but if I select the state and the second drop down appears and I change the state without selecting a city it "resets" like I want it to but not if I've already selected a city. does that make sense? So I'm not sure how to make it work. This is the code I have:
Code:
<select id = "opts" onchange = "showForm()"> |
You have a lot of code there for trying to do something relatively simple.
Take a look at http://www.felgall.com/jstip120.htm for an alternative approach that needs far less code. |
All that repetition of code indicates that you can simplify it a good deal, as felgall indicates.
Shorten the very many instances of document.getElementById() to just one character function $(id) {return document.getElementById(id) } accessed by e.g. $("showTX").style.display = "none"; Why do you keep repeating var selopt = document.getElementById("opts").value; You should at the outset of the function set all the style display attributes to none, then change the relevent ones to block according to the user's selection made. But the answer to your question is when a state is selected from your dropdown (onchange) reset the selectedIndex of the city dropdown to 0. That forces the user to select a state before a city. IMO not very good idea to assign the id opts to a select list when there are several. The Jews were a proud people and throughout history they had trouble with unsympathetic genitals. - Pupil's answer to Catholic Elementary School test. |
Quote:
|
What is the point in having selectCA and then ignoring it unless the selected option is Northern California?
And it's silly to have just two options in Texas and Nevada. Why not just do something like this: Code:
<select name="regions" onchange = "changeDisplay(this.value)">Such as: Code:
<style type="text/css">Code:
function changeDisplay(toRegion)If you really feel you must have the CA, NV, TX <select>s, we can incorporate them. But please, not with the ugly code you used. |
Thanks for the well explained response! I could do that but I liked the idea of having two separate drop downs just so its a little more clear. Haha I know my code stinked that's what I'm here for :) ha
|
We *can* do the extra drop downs. It's not that much more work.
But the "trick" of embedding the info about the regions in the <span> (or similar) is still the right answer for that part of it, I think. |
Yeah I think that's definitely better than what I have. How would I incorporate the multiple drop downs?
|
I already posted a link to a page showing all the code necessary to do this in my first reply. If you had looked at that page then you would see how to get rid of the unnecessary repetitive code.
|
I guess I knew I was going to regret saying that. <grin/>
Code:
<!DOCTYPE html> |
Quote:
|
Quote:
|
Quote:
|
Quote:
If you want to use dynamic effects in your web page then you really ought to learn JavaScript as that is what all such effects are written using. Only if you are going to use effects where someone has already written the entire script for you can you avoid needing to know JavaScript in order to add effects to your page. Trying to add effects to your web page without first learning JavaScript is like trying to fly a plane without taking any flying lessons. |
| All times are GMT +1. The time now is 05:55 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.