0

I'm trying to call a function in two different event handlers. The function runs, but doesn't seem to recognize the passed variable ( or maybe the variable isn't getting passed). It just triggers the alert "nothing".

I'm not understanding how to use console.log() or where exactly to look and see what's going on?

Right now, I put it in the function, like I'm showign below. When I test the page in firefox, I go to Firebug, and click on console. I clear everything, then trigger the event, and I'm not sure what I'm looking at... or if I'm even looking in the right place. Or if I even put console.log() in the right place?

Here's what I have

function pic_shift(){

$("#testit").removeClass("main_page");

    if ( $(this).hasClass('active') ) {

            /*  do stuff  */
        } else if ( $(this).hasClass('one') || $(this).hasClass('ee')) {

        /*  do other stuff  */

        } else {        
            alert('nothing');
            console.log($(this));
        };

}

$('.square-container,.main-nav,.reg3,.privacy_policy,.subnav').on('click', '.square','.main', function(event)  {    

    pic_shift( $(this));        

});

:

When I look at console, I see in "all" this: Object[Window #]

When I look at errors, I see nothing.

I should add that when the function isn't separated out and called, it works perfectly fine, and knows what 'this' is.

Edited by turpentyne: adding note at end

2
Contributors
1
Reply
14
Views
4 Years
Discussion Span
Last Post by McLaren
0

but doesn't seem to recognize the passed variable ( or maybe the variable isn't getting passed)

first thing I see - its not passed. I mean you pass it, but you have to have it in funciton parameters:

your code:

function pic_shift(){

with parameter should be something like:

function pic_shift(param){

then I assume you want to do actions with the thing you passed as parameter. So probably code should look like this:

    function pic_shift(param){

$("#testit").removeClass("main_page");
    if ( param.hasClass('active') ) {
            /*  do stuff  */
        } else if (param.hasClass('one') || param.hasClass('ee')) {
        /*  do other stuff  */
        } else {        
            alert('nothing');
            console.log(param);
        };
}

Not tested, but probably you get the idea.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.