2

Let's first take a look at JavaScript code =>

function ConstructNodes(className,hiddenId,alertMsg,formName){
    this.className = className;
    this.hiddenId  = hiddenId;
    this.alertMsg  = alertMsg;
    this.formName  = formName;
}

ConstructNodes.prototype.getId = function(){
    var nodes = document.getElementsByClassName(this.className);
    for (var i=0;i<nodes.length;i++){
        nodes[i].onclick = function(){
            var r = confirm(this.alertMsg);
            if (r==true){
                alert(this.hiddenId); // undefined
            } else {
                return;
            }
        };
    }
};

var obj = new ConstructNodes("className","hiddenId","Are you sure ?","formName");
obj.getId();

My problem in this situation is that defined objects are undefined under getId's anonymous function , how can I solve this situation ? thanks

9
  • 2
    possible duplicate of this in event handlers for another object Commented Aug 22, 2012 at 22:05
  • @FelixKling how it is duplicate when I've nothing to do with implementing classes Commented Aug 22, 2012 at 22:09
  • @Tornike , your function ConstructNodes is the class Commented Aug 22, 2012 at 22:10
  • @caligula sure , but I've not another class Commented Aug 22, 2012 at 22:10
  • 2
    @Tornike the problem is that your code expects this inside the event handlers set up by the "getId()" function to be the same as it is outside the handlers. It will not be. Commented Aug 22, 2012 at 22:10

3 Answers 3

4

Your code is incorrectly assuming that this will refer to a "ConstructNodes" object inside the event handlers. It won't; it'll be the element. Instead, store this in an object, and things will be better:

ConstructNodes.prototype.getId = function(){
    var nodes = document.getElementsByClassName(this.className), obj = this;
    for (var i=0;i<nodes.length;i++){
        nodes[i].onclick = function(){
            var r = confirm(obj.alertMsg);
            if (r==true){
                alert(obj.hiddenId); // undefined
                document.getElementById(obj.hiddenId).value = this.id;
                alert(obj.hiddenId);
            } else {
                return;
            }
        };
    }
};

(It's not at all clear what you're trying to do, so there may be some issues still.)

Sign up to request clarification or add additional context in comments.

Comments

1

It took me a minute to understand your question.

You cannot refer to this in an anonymous function in getId. you must save this into a variable for example var me = this outside the anonymous function, and then use me.hiddenId.

Here is a JSFiddle to demonstrate that.

2 Comments

That is completely irrelevant.
You are right - I misunderstood the question. I fixed the answer and the JSFiddle.
1

It's undefined because you can not access this in that function just like that. Any function is called with it's own execution context, thus the this in that case is something different than outside of the function.

You can solve that by defining a var in the outer scope and access it in the inner, like this:

function ConstructNodes(className,hiddenId,alertMsg,formName){
  this.className = className;
  this.hiddenId  = hiddenId;
  this.alertMsg  = alertMsg;
  this.formName  = formName;
}

ConstructNodes.prototype.getId = function() {
  var _this = this; // a reference that will be known in your closure/function
  var nodes = document.getElementsByClassName(this.className);
  for (var i = 0; i < nodes.length; i++) {
    nodes[i].onclick = function(){
      if (confirm(_this.alertMsg)) {
        alert(_this.hiddenId); // no longer undefined
      } else {
        return;
      }
    };
  }
};

var obj = new ConstructNodes("className", "hiddenId", "Are you sure?", "formName")
obj.getId();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.