View Full Version : Need some basic JS help with scope
Joche
24 Mar 2007, 10:35 AM
This is my simple code. My question is easy to ask, harder to answer: How can I set the variable editor from where I'm trying to do it. I have tried all kinds of this, and scopes etc but no luck.
var editor;
formTag.select('div.s-form-editor').each(function() {
editor = new Ext.HtmlEditor(this,{toolbar:true});
});
Animal
24 Mar 2007, 10:46 AM
When you say "no luck", what do you want to happen? That code will set the editor variable to a new value for each occurence of a div with the class "s-form-editor". Obviously, after the loop has finished, you'll be able to access the last created HtmlEditor.
Joche
24 Mar 2007, 11:02 AM
When you say "no luck", what do you want to happen?
Thanks alot for your quick reply, it's really annoying getting stuck like I did.
Either I'm brilliant or stupid. When I posted my code I cleaned it up a bit to make it more readable. This made it work a bit better than before. From start it was like this: (And yes, I only count on one single element in the array.
this.editor=null;
// Render the editor (There should just be one editor);
formTag.select('div.s-form-editor').each(function() {
this.editor = new Ext.HtmlEditor(this,{toolbar:true});
});
Now I will just do even if it's not as clean as code other guys write! :-)
this.editor=null;
// Render the editor (There should just be one editor);
formTag.select('div.s-form-editor').each(function() {
editor = new Ext.HtmlEditor(this,{toolbar:true});
});
this.editor=editor;
Animal
24 Mar 2007, 10:17 PM
Read the docs on each()!
Scope defaults to the element the iteration is on.
You can specify the scope in the each call, so
// Render the editor (There should just be one editor);
formTag.select('div.s-form-editor').each(function() {
this.editor = new Ext.HtmlEditor(this,{toolbar:true});
}, this);
SteveEisner
25 Mar 2007, 8:29 AM
Wouldn't that be something like:
// Render the editor (There should just be one editor);
formTag.select('div.s-form-editor').each(function(el) {
this.editor = new Ext.HtmlEditor(el,{toolbar:true});
}, this);
? Or maybe I'm misunderstanding the point of the loop
Animal
25 Mar 2007, 8:39 AM
Yes. Well spotted!
Must.... resist.. temptation to edit....!
Powered by vBulletin® Version 4.1.5 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.