Possible Duplicate:
jQuery/JavaScript “this” pointer confusion
var timeOut = 1000;
$('#'+form +' :input').each(function() {
thisObj = "";
if ($(this).parent().parent().find(".incorrect").css('display') == "inline-block" || $(this).attr('value') == "") {
if ($(this).attr('id') != "submitbtn") {
thisObj = $(this);
submit_this++;
colourfading(this, 'rgba(56, 183, 0, 1)', 'rgba(255, 30, 0, .2)', 'rgb(210,210,210)', 'rgb(250,250,250)');
}
}
var delaySubmit = (function(){
var timer = 0;
return function(callback, ms){
setTimeouts(callback, ms, thisObj);
};
})();
delaySubmit(function(){
do_sendRequest(ajaxObj, thisObj, true);
}, timeOut );
timeOut = timeOut + 1000;
});
This is some code I use when pressing a submit button on a form.
You can see that the function colourFading uses "... (this, ' ...". The this element is the input element from the '.each(function() {' call.
It's doing a perfect job there in the colourfading function I wrote, each input element gets a nice red colour indicating that there's something wrong with it and then fades back to white.
Now comes the "fun" part. As you can see I've linked the 'thisObj' variable as the same 'this', thats getting passed to colourFading,
Yet, when I look at .attr('id') of thisObj it indicates that the 'this' is in fact the representation of the submitbutton I clicked.
This is very strange, since when I ask for the id from the 'this' thats being send to colourfading (in the colourfading function) it nicely displays the id of the input element.
I've tried setting thisObj = this, thisObj = $(this), right after the start of the .each(function() { part, right before colourfading where it seemed it had the right element, it's just not working.
Am I doing something wrong here? Shouldn't it work this way???