Hello. The task is this - you need to assign the badge-danger class when clicking on a link to the sister span element, at the same time if the sister elements of other links in the list have badge-danger - assign the badge-info. I wrote 2 functions, but they work only separately, if together - then only a badge-danger is assigned with a click, and where it already is - it is not reset.

$("#brain_list li a").click( function () { if ($('#brain_list li span').hasClass("badge-danger")) { $('#brain_list li span').removeClass("badge-danger") $('#brain_list li span').addClass("badge-info") } }, function () { $(this).siblings('span').addClass("badge-danger") } ); 

    2 answers 2

    decided on my own

     $("#brain_list li a").click( function () { $("#brain_list li a").siblings('span').not(this).removeClass("badge-danger").addClass("badge-info"); $(this).siblings('span').removeClass("badge-info").addClass("badge-danger"); } ); 
       $("#brain_list li a").click( function () { $(this).siblings('span').addClass("badge-danger"); $("#brain_list li a").each(function () { var t = $(this); if (t.hasClass("badge-danger")) { t.removeClass("badge-danger") .addClass("badge-info"); } }); }); 

      if understood correctly, then it should work

      • does not work. Or rather, it works the same way as I do — it assigns the class badge-danger where it is not there, and it does not assign badge-info, so where is the badge-danger. It turns out that with a click each element changes its class only to badge-danger, but not one to badge-info. - Denis
      • did edit, try - Valeriu Vodnicear
      • No, the result is the same. I figured it out myself, but thanks anyway - Denis
      • please, you can write your answer, maybe someone will be useful - Valeriu Vodnicear