1

I'm working on changing prompt() to jPrompt() since IE blocks prompt() from running. The problem is $(this) no longer works correctly because jPrompt() doesn't return a value, but uses a callback function.

So lets say I have something like this, which works:

$("a.foo").click(function(){
    $(this).text(prompt("Type Something",""));
}

When I convert it to this it breaks:

$("a.foo").click(function(){
    jPrompt("Type something:","","", function(r) {
        $(this).text(r);
    }
}

How do I access $(this) properly?

3 Answers 3

5

Try this:

$("a.foo").click(function(){
    var that = this;
    jPrompt("Type something:","","", function(r) {
        $(that).text(r);
    }
}
1

You could use a closure:

$("a.foo").click(
  function(){
    var self = this;
    return function() {
      jPrompt("Type something:", "", "", function(r) { 
        $(self).text(r); 
      });
    }
  }()
);
0

The problem is that you're trying to access the 'r' as an element. jPrompt is going to pass the text entered as 'r'.

$("a.foo").click(function(){
    jPrompt("Type something:","","", function(r){
        alert(r); //This will show the text entered.
    });
});

Not the answer you're looking for? Browse other questions tagged or ask your own question.