0

Im confused and my head is sore, please help.

I am trying to send a variable to fancybox.js when an element has a class 'selected'. This works:

var showMe = true;

This also works:

if(a){
  var showMe = true;
}

This results in an alert saying "class is selected":

if($('#shuffle').hasClass('selected')){
  alert("class is selected"); 
}

Ive also checked in Firebug and the class is selected.

The problem is when I do this it doesn't work:

if($('#shuffle').hasClass('selected')){
    var showMe = true;  
}

showMe is not set to true.

I know it must be simple, thanks in advance for any help.

EDIT Here is what worked: For some reason, the class changing to selected didn't set the showMe = true. I added an .click function to the button instead and it did work. Not sure why, but if you know where I should be reading into this then please past the link in here.

$(function() {
  $("#shuffle").click(function(){

    showMe = true; 
    console.log( showMe);
  });
});

Thanks for everyone that responded, Im guessing the reason your answers didn't work was something to do with the way i asked the question. Cheers

4 Answers 4

2

By using var showMe = 'true' you are setting a local variable. If you want to use that variable outside the function, just use showMe = 'true' < that will set a global variable.

Like this

if($('#shuffle').hasClass('selected')){
    showMe = true;  
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for responding, this code also didnt give me the desired result. I solved the issue by attaching to an event, im sure there is some correct terminology to use here but I dont know it, maybe it was presumed that I would be attaching to an event, im not sure. I will paste the code that solved it in my post, thanks for replying though, im guessing the reason is more to do with my lack of experience in defining the question.
1

Maybe you'll have a look at this:

var showMe = ($('#shuffle').hasClass('selected')) ? true : false;
alert(showMe);

Be sure the DOM is ready when you're running that!

2 Comments

$(function() { var showMe = ($('#shuffle').hasClass('selected')) ? true : false; alert(showMe); }); with a <div id="shuffle" class="selected"></div> works fine for me (alerts true)
I also get the alert, the problem is the showMe is not set to true.
1

Declare your variable before the document ready or whatever. That way you will make it global.

<script type="text/javascript">
    var showMe = false;
    jQuery(document).ready(function($) {
        console.log( showMe );
        if ( $('#shuffle').hasClass('selected') )
        {
            showMe = true;
            console.log( showMe );
        }
    });
</script>

Comments

0

try

if($('#shuffle').hasClass('selected')){
    showMe = true;  
}

i have moved the showMe to global scope(by removing the var keyword), now where ever you are accessing it use it like

if(typeof(window.showMe) != 'undefined') 
 alert(showMe);

1 Comment

Thanks for responding, this code also didnt give me the desired result. I solved the issue by attaching to an event, im sure there is some correct terminology to use here but I dont know it, maybe it was presumed that I would be attaching to an event, im not sure. I will paste the code that solved it in my post, thanks for replying though, im guessing the reason is more to do with my lack of experience in defining the question.

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.