-4

For some reason I can't seem to alert the value of a variable, my variable is global and defined at the top of the page...

var slickTog = 'dsadsdasd';

I then have a window resize function that I have the alert in only it doesn't work!

function windowSize(){
    alert(slickTog);
    ...
}

windowSize();

$(window).on('resize',function(){
    windowSize();
});

Does anybody have an idea of why this isnt working? I've attached a jsFiddle too...

https://jsfiddle.net/7024manL/

Liam
  • 9,725
  • 39
  • 111
  • 209

3 Answers3

4

You've defined slickTog both in the global scope, and that functions scope. The alert will use the function scope first, which is why you are not getting the behaviour you expect.

On line 18 of your fiddle, remove the var so it's just:

slickTog=true;
millerbr
  • 2,951
  • 1
  • 14
  • 25
2

You re-define slicktog

Change var slickTog = true to just slickTog = true

Jordan Lowe
  • 368
  • 1
  • 13
1

your variable slickTog is being hoisted in function windowSize

var slickTog = 'dsadsdasd'; 
function windowSize(){
alert(slickTog);
  switchImg();
  if($(window).width()<768){
    $('.active-pane .carousel').attr('data-interval','true');
    $('.active-pane .carousel').carousel({interval:5000});          
    $('.filtering').slick({
      slidesToShow: 1,
      slidesToScroll: 1
    });     
    var filtered = false;

    slickTog=true;  //was declared var slickTog = true;

    $('.js-filter').on('click', function(){
      if (filtered === false) {
        $('.filtering').slick('slickFilter',':even');
        filtered = true;
      } else {
        $('.filtering').slick('slickUnfilter');
        filtered = false;
      }
    });    
  } else {
    $('.active-pane .carousel').attr('data-interval','false');
    alert(slickTog);
  }  
}

windowSize();

$(window).on('resize',function(){
    windowSize();
});
Vicky Gonsalves
  • 11,593
  • 2
  • 37
  • 58