Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
| Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 7th, 2006, 04:49 AM
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])
        }
    }
  }
</script>
--- and later followed by HTML
  <select name="svcProvider" size="6" id="svcProvider" onchange="updateSpecialty(this.selectedIndex)">
      <option >a</option>
      <option >b</option>
    </select>

I would greatly appreciate any feedback.
Thank you.


  #2 (permalink)  
Old January 7th, 2006, 08:18 AM
Friend of Wrox
 
Join Date: May 2004
Location: , , .
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is svcSearch your form name, and specialty the name of the dropdown? Just wondering if its a simple error like spelling. Can you post the complete code?

Joe

  #3 (permalink)  
Old January 7th, 2006, 10:27 PM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, the form id is "svcSearch" and the select box to be filled out by Javascript is named "specialty" as shown below.
The problem is that the global var speclist isn't seen by the function updateSpecialty. It works if declared locally, meaning that spelling isn't the problem. Thanks for looking at this with me.


<script type="text/javascript">
  var speclist=document.svcSearch.specialty // global variable
  function updateSpecialty(selectedProviderIndex) {
      // var speclist=document.svcSearch.specialty -- works if declared locally
      var specs=new Array();
      specs[0]=""
      specs[1]=["X1|x1", "Y1|y1"]
      specs[2]=["X2|x2", "Y2|y2"]

      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])
        }
    }
  }
</script>
--- and later followed by HTML
<form id="svcSearch" name="svcSearch" method="post" action="Results.php">

    <select name="svcProvider" size="6" id="svcProvider" onchange="updateSpecialty(this.selectedIndex)">
      <option>a</option>
      <option>b</option>
    </select>

    <select name="specialty" size="6" multiple="multiple" id="specialty">

    </select>

    <input type="submit" name="Find" value="Find"/>

</form>



Similar Threads
Thread Thread Starter Forum Replies Last Post
scope of variables in VB.NET bhavna General .NET 1 January 26th, 2007 06:00 PM
Using VBScript variables in Javascript PL Classic ASP Basics 4 July 29th, 2004 03:05 PM
Scope of recordset variables James Diamond Classic ASP Databases 2 January 28th, 2004 09:37 AM
How Do I Pass 2 Variables in Javascript Lucy Javascript 2 December 13th, 2003 11:25 AM
equating javascript and ASP variables GregH Classic ASP Basics 3 July 3rd, 2003 02:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.