Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery.Click() Doens't work after it's used one time

I have a really simple jquery.Click() function

//hide divs
$("#a-1").hide();

$(".v-1").click(function(){
    $("#a-1").show();
    $(".v-1").addClass("c-it");
    $(".c-it").click(function(){
        $("#a-1").hide();
        $(".v-1").removeClass("c-it");
    });
});

So when v-1 is clicked a-1 has to show and it will add an class named c-it. That works after that i click c-it , a-1 will hide again and the c-it class will be removed. So now we are the beginning again but now when i click v-1 nothing happens someone knows why?

Html

<i class="glyphicon glyphicon-plus v-1">&nbsp;<span>This is a question</span></i>
        <div id="a-1"><p>Awnser</p></div>
like image 441
djamaile Avatar asked Jan 22 '26 04:01

djamaile


2 Answers

just use toggle() and toggleClass() better for you

$("#a-1").hide();

$(".v-1").click(function() {
  $("#a-1").toggle('hide');
  $(".v-1").toggleClass("c-it");

});
like image 141
elreeda Avatar answered Jan 24 '26 17:01

elreeda


You should use a single click handler that simply toggles the visible state and the class.

$(".v-1").click(function(){
    $("#a-1").toggle();
    $(".v-1").toggleClass("c-it");
});
like image 27
Louis Avatar answered Jan 24 '26 18:01

Louis