Give hide or show when clicking another element

1

I have 4 threads, and I would like to display only what I clicked, and if another is open close, I know how to do this with jquery, but I'm doing it in a way that I do not think is more "correct", I leave the code too long putting too many constraints and so on.

<a class="topico1">
<a class="topico2">
<a class="topico3">
<a class="topico4-exibir">

I would like to click on another topic it earns the display value, and the one with this value currently loses it. I in the case would do a function in the click, that when clicking on the class topico3, check if it has the sub class display, if it is remove the class sub class display from all the others, and if it does not it adds and removes from all other , so in case I would do this same function for all classes, this takes a lot of time and code, how would I do it in an easier way? I hope it has not been too complicated to understand.

    
asked by anonymous 02.10.2017 / 22:37

1 answer

3

You do not need more than 4 lines to do this, add a common classname to the elements, and do this.

$(document).on('click','.topic',function(){
    $('.topic').removeClass('exibir');
    $(this).addClass('exibir');
});

Functional example in fiddle: link

    
02.10.2017 / 22:43