Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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.


Reply With Quote
  #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

Reply With Quote
  #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>

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 05:19 AM.


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