0

Hi I want to understand how function return value. I have two function one is working and second is not working. I want to know why is second function not working. http://jsfiddle.net/95vXQ/5/

enter image description here

Here is second function which is not working. I want to why it is not changing isVisible value

enter image description here

2
  • Can you post the link to the jsFiddles or the source code? Commented Aug 27, 2012 at 10:57
  • I have edited my question, please find jsfiddle link. jsfiddle.net/95vXQ/5. Commented Aug 27, 2012 at 11:00

5 Answers 5

2

jQuery click function is explained clearly in this link. The click function can be used to trigger a single or a series of jQuery events on click of an object. The variable declared in the function will be functional only within that click function. Values cannot be returned in a click function and only events can be triggered.

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

Comments

2

The second screenshot does not work because isVisible is defined inside the click function. Once that function is complete isVisible gets unset. When you call the click function again, isVisible gets reset and it's value back to true. That is called variable scope.

And this problem has nothing to do with returned values. To return a value you'd have to use return [varname], and that would not help inside a click function.

5 Comments

yes thats I want to know why isVisible not returning value inside the click function
jQuery click function can only execute the series of events defined within them. To return a value an independent function needs to be defined which can be called using the function name. And as given in this answer the scope of the variable will be only inside the click function.
@amit This answers your question, what else do you want to know?
@undefined: i want to why value is not returning to isVisible when it is inside the click event
@amit Because when the handler is called you set the value to true. So only the first condition is executed. nothing more.
2

It is variable Scope problem. Find more info here. To keep it simple, any variable that you defined inside a function is undefined outside the function.

And the function in second screenshot is failing because that function gets executed everytime there is a click event i.e., even if you are resetting isVisible value, it won't be carried to next call since you are initializing isVisible again with a true value set to it.

Comments

2

Your second example doesn't work as expected, since inside the function you are re-assigning a value true to isVisible; and note that scope of isVisible will be only inside the function, if you are declaring it inside click function.

If you just want to know the working of above examples, and not interested in declaring variable outside the function, then try this jsfiddle.

If you want to do a show/hide functionality, then try this toggle jsfiddle. Code below.

$(function(){
    $("#btnShow").toggle(
        function(){
            $(this).val("Show");
            $("#divContent").hide();
        },
        function(){
            $(this).val("Hide");
            $("#divContent").show();
        }
    );    
});

Comments

0
+50

Very Simple.. Amit.

  1. Button = Hide; Div content is shown
  2. click Hide; Now IsVisible is True; and it will hide; Button = Show; Div is hidden
  3. Now, Click show. the Click handler executes the code again and creates a variable IsVisible as new one and it is set to true. It hides the div(which is already hidden) and button still shows Show.

In your first case, it is a global variable so, once it is updated to False after hiding , it shows the div again.

Still not clear?

2 Comments

click function return isVisible value to, when it was declare as a global variable but why it is not working when we take that variable to inside click event
You itself has mentioned that it is a global variable. When u take it to a function scope, then it is a local variable which will be defined as a new one whenever Click action has been made

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.