/    Sign up×
Community /Pin to ProfileBookmark

Scope Problem?

I’m getting the error (in Firebug):

[CODE]this._OptionsForm.SelectName is undefined[/CODE]

But the variable is defined…

[code=html]
<script>
// START ChainedSelectBuilder Class
function ChainedSelectBuilder() {}

// Declare Member Variables
ChainedSelectBuilder.prototype._FormName = ‘SomeForm’; // The Name of the Form to use
ChainedSelectBuilder.prototype._OptionsForm = ‘document.’+this._FormName;;

// Member Functions
ChainedSelectBuilder.prototype.RebuildSelectOptions = function(SelectName, OptionsArray)
{
// Reset the Options Array
this._OptionsForm.SelectName.options.length=0
}
</script>

[/code]

Help?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Aug 03.2008 — 'document.'+this._FormName doesn't create a reference to that form, it just creates a string of value "document.this.SomeForm" It's like trying to say"document.this.SomeForm".SelectName.options.length=0
. You need<i>
</i>ChainedSelectBuilder.prototype._OptionsForm = document[this._FormName];

Also, give your script tag a type of "text/javascript".<i>
</i>&lt;script type="text/javascript"&gt;
.
Copy linkTweet thisAlerts:
@lightnbauthorAug 03.2008 — Thanks,

I corrected the line. A couple questions:

Aren't square brackets used for array indexes? So is "document" actually an array? And how do you know when to use a brace and when to use a dot?

It's still giving me the error though... I'm wondering if "this" inside of a function is referring to the function, and not the parent class?

I now have:

[code=php]
function ChainedSelectBuilder() {}

// Declare Object variables
ChainedSelectBuilder.prototype._FormName = 'SomeForm'; // The Name of the Form to use
ChainedSelectBuilder.prototype._OptionsForm = document[this._FormName];

// FUNCTIONS
ChainedSelectBuilder.prototype.RebuildSelectOptions = function(SelectName, OptionsArray)
{
// Reset the Options Array
this._OptionsForm.SelectName.options.length=0;
}
[/code]


I also tried:

[code=php]
[this._OptionsForm]SelectName.options.length=0;
[/code]
Copy linkTweet thisAlerts:
@Declan1991Aug 04.2008 — No, document is an object. You can access an object properties in two different ways// 1
document["getElementById"]("idname");
// 2
document.getElementById("idname");
The important thing about the first one is that it takes a string.

What I'd say is happening is that this refers to the global object (window) when you are assigning the prototype. So this should do it.<i>
</i>document[this._FormName][SelectName].options.length=0;
SelectName is a string, so you have to use the other way of accessing a property.
Copy linkTweet thisAlerts:
@lightnbauthorAug 04.2008 — Now I'm getting the error:

[CODE]document[this._FormName] is undefined
ChainedSelectBuilder.js
Line 12[/CODE]
Copy linkTweet thisAlerts:
@lightnbauthorAug 04.2008 — And I'm using:

[code=php]document[this._FormName][SelectName].options.length=0;[/code]
×

Success!

Help @lightnb 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.18,
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,
)...