1

I have a problem, When I click on the link p open, and then I click in the other link it doesn't work. It works when I click again.

This is my jQuery code:

var toggle = 0;
$(document).ready(function () {
    $(".feedback_block").each(function () {
        $("a", this).click(function (e) {
            if (toggle == 0) {
                $(this).parent().children("p").stop(true, true).fadeIn(500);
                $(this).addClass("clicked");
                $(this).children().addClass("clicked_span");
                toggle = 1;
                console.log(toggle);
            } else
            if (toggle == 1) {
                $(this).parent().children("p").stop(true, true).fadeOut(500);
                $(this).removeClass("clicked");
                $(this).children().removeClass("clicked_span");
                toggle = 0;
                console.log(toggle);

            }
            e.stopPropagation();
            return false;
        });
        toggle = 0;
    });
});

When I click the parameter toggle get 1 and when I click another link the initial value should be 0. How can i do it?

My example: http://jsfiddle.net/amkrtchyan/tjzaR/

1
  • I've edited this, but the S/O editor refused about 10 suggested titles (infuriating). Anyone, please improve my (forthcoming) edit. Commented Mar 20, 2012 at 10:38

2 Answers 2

1

You could simply use toggle() (http://jsfiddle.net/tjzaR/2/)

$(document).ready(function () {
    $(".feedback_block").each(function () {
        $("a", this).toggle(function (e) {

                $(this).parent().children("p").stop(true, true).fadeIn(500);
                $(this).addClass("clicked");
                $(this).children().addClass("clicked_span");

            }, function(){
                $(this).parent().children("p").stop(true, true).fadeOut(500);
                $(this).removeClass("clicked");
                $(this).children().removeClass("clicked_span");


            });
    });
});

or you could do (fiddle here http://jsfiddle.net/tjzaR/1/)

$(".feedback_block").each(function () {
    $("a", this).click(function (e) {
        if (!$(this).parent().children("p").is(":visible")){
            $(this).parent().children("p").stop(true, true).fadeIn(500);
            $(this).addClass("clicked");
            $(this).children().addClass("clicked_span");

        } else{
            $(this).parent().children("p").stop(true, true).fadeOut(500);
            $(this).removeClass("clicked");
            $(this).children().removeClass("clicked_span");


        }
        return false;
    });

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

3 Comments

I know the function toggle(), but there is a way to do this in my code?
@AramMkrtchyan just remove your $("a", this).click(function (e) { with the code above, and remove the toggle variable you created.
The first method documented here is now deprecated as of jQuery Version 1.9 - Just a heads up
0
 $("a", this).toggle(function () {

                $(this).parent().children("p").stop(true, true).fadeIn(500);
                $(this).addClass("clicked");
                $(this).children().addClass("clicked_span");
                return false; 

            }, function(){

                $(this).parent().children("p").stop(true, true).fadeOut(500);
                $(this).removeClass("clicked");
                $(this).children().removeClass("clicked_span");
                return false;

            });

Can we not directly use the toggle function. Toggle

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.