Help - Search - Members - Calendar
Full Version: Javascript change selection?
Invision Power Services > Community Forums > Community Web Design and Coding
Kenny Pollock
Hey guys!

Question: I have a drop down, that when you change it and select another client from it, it fills the fields (name, address, city, state) with the information for the client. It works great, although I don't know how to change the selection in the states drop down.

Example: Kenny is in Hollywood, FL. When you select Kenny from the drop down, it fills the city input field with Hollywood, but isn't changing the states drop down with AL, CA, FL, etc. with the selection of FL, which is Kenny's state.

Any solution?
Chuck_Hicks
when you got to print the state dropdown do make sure you put selected in the option of the clients state.
Kenny Pollock
Okay but how can I have Javascript do this?
Chuck_Hicks
After playing around with the sample on w3cschool.com I think i got a sample that should point you in the right direction.

CODE
<html>
<head>
<script type="text/javascript">
function getOptionIndex()
{
var x=document.getElementById("mySelect")
x.selectedIndex = 1;
}
</script>
</head>

<body>
<form>
Select your favorite fruit:
<select id="mySelect">
    <option>Apple</option>
    <option>Orange</option>
    <option>Pineapple</option>
    <option>Banana</option>
</select>
<br /><br />
<input type="button" onclick="getOptionIndex()" value="Alert index of selected fruit">
</form>
</body>
Kenny Pollock
I think you get what I mean a little, and now I know how to do what I want almost... just tell me this: selectedIndex is the order it appears on the list. So if A was first in the list, and B was second, A would be 1 and B would be 2. How can I have it select 23 for Florida? If I want Florida selected, I have to give it a number to get to Florida...
Chuck_Hicks
A would be index 0 and B would b index 1. It works just like an array does.
Kenny Pollock
Yeah but without doing if ( Florida) { selectedIndex = 23 } if ( Alabama ) { selectedIndex = 0 } how would I do this?
Chuck_Hicks
Actually i am going tochange my post from asking how you are getting the client info to the following.

You know how many states there are it's constent. So all you have to do is do a for loop.

CODE
for(i=0; i<=50; i++)
{
    if(x.options[i].txt == state)
    {
        x.selectedIndex = i;
        break;
     }
}


sorry if my answers are alittle hard to understand. My head is really twisted around this html logic thing i am trying to do. I know it's posible i am just not as good as matt lol.
Kenny Pollock
Sorry for taking so long to reply... I can't seem to get it working.

CODE
    if (http.readyState == 4)
    {
        results = http.responseText;
        var brokenstring = results.split("!split!");
        document.getElementById('origin_company').value = brokenstring[0];
        document.getElementById('origin_contact').value = brokenstring[1];
        document.getElementById('origin_address').value = brokenstring[2];
        document.getElementById('origin_city').value = brokenstring[3];
        document.getElementById('origin_zip').value = brokenstring[5];
        document.getElementById('origin_phone').value = brokenstring[6];
        document.getElementById('origin_cell_phone').value = brokenstring[7];
        
        for(i=0; i<=50; i++)
        {
            if(document.new_order.origin_state.options[i].txt == brokenstring[4])
            {
                document.new_order.origin_state.selectedIndex = i;
                break;
             }
        }
    }
Kenny Pollock
No one?
Brendon Koz
How about an example?
http://www.dhtmlgoodies.com/index.html?pag...ed_select_boxes

I think the above link will point you in a closer direction. There are some caveats with select boxes apparently...if you're up to trying to see what the heck this code is doing, be my guest (supposedly something takes focus or precedence...I didn't dissect the code to understand it):
http://saloon.javaranch.com/cgi-bin/ubb/ul...20&t=006204
Kenny Pollock
I couldn't get anything working... I hate working with JavaScript!!!

GreenElephants example worked for me once, and after I started incorporating it, it stopped. I've triple checked everything, it should be working!
Kenny Pollock
*bump*
Brendon Koz
Having the JS without the accompanying HTML doesn't help a lot of us to figure out what's wrong. I personally like to debug when I don't know a language like the back of my hand - JavaScript goes under that category. original.gif

If the data from the files are somewhat private, dummy data using the same scheme should work. wink.gif ...if it's truly that important to you. Just zip it all up and upload it.
Kenny Pollock
You got a PM! I would post the code, but it needs to use the db for the data, so it wouldn't work right.
Brendon Koz
*.sql in attachment. tongue.gif
Brendon Koz
FIXED.

GreenElephant led you astray accidentally:
CODE
*********.txt = *******;


It's text, not txt.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.