Can anyone tell me why the scope of $mytextarea, in the code below, doesn't extend into the getAnswerToo() function?
getAnswer() works because I'm explicitly selecting the element I want updated. However, trying to use a global 'handler' variable doesn't work.
I'm assuming that this problem with the scope of the variable is specifically related to the fact that it holds a jQuery object. The other variable, myurl, works fine.
Any help much appreciated!
var myurl = "php/getAnswer.php";
var $mytextarea = null; // a handler for the textarea element
$mytextarea = $('textarea#mytextarea');
getAnswerToo();
function getAnswer(){ // works
var request = $.ajax({
url: myurl,
type: "POST",
data: {question: questionId, user: userId},
dataType: "html",
success: function(data) {
$('textarea#mytextarea').val(unescape(data));
}
});
}
function getAnswerToo(){ // doesn't work
var request = $.ajax({
url: myurl,
type: "POST",
data: {question: questionId, user: userId},
dataType: "html",
success: function(data) {
$mytextarea.val(unescape(data));
}
});
}