It’s a scoping issue.  ‘this’ refers to the object in the current scope, inside of the inputs.each, ‘this’ refers to the element of the array.  Try doing something like:

 

var realThis = this;

                        inputs.each( function(input) {

                                    try {

                                                if(validInputs.indexOf(input.id) != -1) {

                                                            Event.observe(input,'blur',realThis.alertBox.bind(realThis),false);

                                                }

                                    } catch(e) {

                                                alert(e);

                                    }

                        });

 

Greg

 


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Whitcraft, Jon
Sent: Thursday, March 16, 2006 8:56 AM
To: rails-spinoffs@lists.rubyonrails.org
Subject: [Rails-spinoffs] This is driving me crazy!!!

 

I have the following code but it keeps on giving me this error when it runs.

 

--- Error: this.alertBox has no properties

 

What would be causing this?  My Code checks out when I call this.alertBox() from the setup function it works fine but when I try and put in on an event observer it doesn’t work.


What do you guys think?

 

Thanks,

 

Jon

 

Code Here:

 

 

var Updater = Class.create();

 

Updater.prototype = {

            initialize: function() {

                        var options = Object.extend({

                                    currentItemId: 0,

                                    debug: false

                }, arguments[0] || {});

                this.options = options;

                this.setup();

            },

            setup: function() {

                        validInputs = ['name','image','headline','caption'];

                        validInputs = $A(validInputs);

                       

                        inputs = document.getElementsByTagName('input');

                        inputs = $A(inputs);

                       

                        inputs.each( function(input) {

                                    try {

                                                if(validInputs.indexOf(input.id) != -1) {

                                                            //input.>

                                                            Event.observe(input.id,'blur',this.alertBox.bind(this),false);

                                                }

                                    } catch(e) {

                                                alert(e);

                                    }

                        });

           

            },

            alertBox: function() {

                        alert("Hello")

            }

}

 

********************

********************

This E-mail (and attachments) may contain confidential/privileged information intended only for the named addressee(s). If you are not an intended recipient, do not read, copy, disseminate or take any action based on the content of this E-mail. Please notify the sender by reply E-mail and erase this E-mail from your system. Your assistance is appreciated. E-mail transmission may not be secure or error-free. The company is not responsible for any loss/damage arising from any virus transmitted.

********************

********************

_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

Reply via email to