1

I just don't get it.

function init() {

    $(document).on('click', '#listFilter .option:not(".darr"), #listSort .option:not(".darr")', function() {
        var selected = $(this).data('ajax-link'),
            dropDown = $(this).parent().parent(),
            filter = '',
            sort = '';

        if ( dropDown.attr('id') == "weaveListFilter" ) {
            filter = selected;
            sort = $('#listSort .darr').data('ajax-link');
        } else if ( dropDown.attr('id') == "weaveListSort" ) {
            filter = $('#listFilter .darr').data('ajax-link');
            sort = selected;
        }

        if ( selected != dropDown.find('.darr').data('ajax-link') )
            console.log('why?')
            sortList(filter, sort, dropDown.parent());
            //console.log('wtf!')
    });

}

Without the console.log('why?') the sortList() function is not called!!! Why does it not get called without this line?

The weirdest thing is, that this "why?" is not even logged in my console. But without it the sortList() function doesn't execute. The "wtf!" after the function-call would be logged but I don't get it anyway.

Ideas? Am I dumb or so?

2
  • Are there any JS errors in the error console? Commented Jul 19, 2012 at 17:05
  • It sounds like the condition selected != dropDown.find('.darr').data('ajax-link') is never true. If nothing gets logged, then console.log('why?') is never executed, either. Try surrounding the console.log call and sortList with { and }. Commented Jul 19, 2012 at 17:11

2 Answers 2

4

Because your if is failing. There are no braces { } around the if body, so only the first statement is considered part of the if. By adding the console.log, you are making the sortList no longer part of the if.

First, put braces around your if:

if ( selected != dropDown.find('.darr').data('ajax-link') ) {
    sortList(filter, sort, dropDown.parent());
}

Then go find out why your if clause is failing.

Sign up to request clarification or add additional context in comments.

2 Comments

Isn't it possible to neglect the {} brackets? If it's intended correctly?
yes, it is, but it is cases like this that makes it helpful to have them, so that mistakes are not made.
3

You forgot the block { } after this if:

if ( selected != dropDown.find('.darr').data('ajax-link') )

This condition is apparently false, so what happens is:

  • console.log() doesn't execute
  • sortList() is not attached to the if, so it always runs.

If you comment console.log() out, neither of the statements runs, because

  • console.log() is commented
  • sortList() is under a false condition

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.