/    Sign up×
Community /Pin to ProfileBookmark

onload doesn’t keep existing data

I have script that will populate a combo box with the cities associated with a zip code. I would like to have this function run when the page loads, but also show the city that was selected already.

I have a clientedit.asp where a user can edit existing data for a client. Therefore, the home info already has info. For example, the homezip is 91607 and the chosen city is Valley Villlage.

If I have the body onload=getcitystate(document.clientedit.HomeZip.value, 1), the city combo box will be populated with the cities associated with 91607 (Sherman Oaks, Studio City, North Hollywood, Valley Village). However, the combo box will default to the first city (Sherman Oaks). Is there a way for me to have the combo box default to the existing data (if it exists) after the onload function?

the javascript function is:
function getCityState(z, sel)
{

var city, state, sZip;
sZip = z;
switch(sel){
case 1:
city = “HomeCity”;
state = “HomeState”;
break;
case 2:
city = “MailCity”;
state = “MailState”;
break;
}

if ((sZip.length > 0) && (trim(sZip) != “”))
{
var sZipDigit = sZip.charAt(0)
var sFileName = “xml/cities” + sZipDigit + “.xml”;
xmlDoc.async=”false”;
xmlDoc.onreadystatechange=verify;
xmlDoc.load(sFileName);
for (var i = 0, length = city.length; i<length; i++)
document.all(city).remove(0)
var oNodes = xmlDoc.selectNodes(“/cities/city[@zip='” + sZip + “‘]”)
for (var oNode = oNodes.nextNode; oNode != null; oNode = oNodes.nextNode)
{

  • *

    * var sCityName = oNode.getAttribute(“cityname”)


  • *

    * var opt = document.createElement(“option”)


  • *

    * opt.value = sCityName


  • *

    opt.text = sCityName
    document.all(state).value = oNode.getAttribute(“statename”)


  • *

    document.all(city).add(opt)
    }

    }
    }

  • to post a comment
    JavaScript

    0Be the first to comment 😎

    ×

    Success!

    Help @nganb spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...