0

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???

Community
  • 1
  • 1
C-TZ
  • 659
  • 1
  • 5
  • 15
  • 3
    well one thing is `thisObj = "";` is a global variable, it probably should be a local. Use `var` to declare variables! – epascarello Jun 19 '12 at 13:58
  • The only places you use `thisObj` is in calls to functions `setTimeouts()` and `do_sendRequest()`, which are nowhere to be seen in your code. – lanzz Jun 19 '12 at 13:59
  • Jeah sorry for that, I didn't think it would matter because I've been working on this for about 3 hours now and I've made myself sure there is nothing wrong, at all, with those functions. They are called from several places in the code and work fine. I think it's a pointer confusion then... I've read the post but don't really understand it. How can I make a var which consists of the 'this' object, as it is called in colourfading? :) – C-TZ Jun 19 '12 at 14:30
  • Ok... after 4 hours, I've given up the part where I use timers to do this... Because of this pointer confusion there is NO WAY I'm going to get this working. Javasript just "forgets" what the f* object is --_--... Going to post rest of code new situation. – C-TZ Jun 19 '12 at 15:22
  • Nevermind, I'm too pissed right now to go and post this. Got better things to do, luckely for me this is out of scope of project, just thought it would be fun if I could check all input fields. Send me a PM of you wanna help me with this because I'm just not getting this anti-logic. – C-TZ Jun 19 '12 at 15:34

0 Answers0