View Single Post
  #1 (permalink)  
Old January 7th, 2006, 04:49 AM
minhtam minhtam is offline
Registered User
Join Date: Jan 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Javascript Scope of Variables.

--- In one HTML file, I have the following section of Javascript code with a global variable "speclist" that is to be accessed inside a Javascript function. The problem: it seems that the function doesn't see the global variable. Did I overlook something really easy? It would work if the variable is declared locally in the function.

Code listing of what doesn't work:
<script type="text/javascript">
  var speclist=document.svcSearch.specialty // global variable
  function updateSpecialty(selectedProviderIndex) {
        // var speclist=document.svcSearch.specialty -- works if declared locally
      speclist.options.length=0 //clear list
    if (selectedProviderIndex > 0) {
        for (i=0; i < specs[selectedProviderIndex].length; i++) {
            speclist.options[speclist.options.length] = new Option(specs[selectedProviderIndex][i].split("|")[0], specs[selectedProviderIndex][i].split("|")[1])
--- and later followed by HTML
  <select name="svcProvider" size="6" id="svcProvider" onchange="updateSpecialty(this.selectedIndex)">
      <option >a</option>
      <option >b</option>

I would greatly appreciate any feedback.
Thank you.