vendredi 1 juillet 2016

Jquery show multiple elements on Click

I have this situation: Two columns with information one with questions and one with answers. I want to click in one question and show in the next column the answer. The question can have multiple answers so I need to show multiple elements with a single click. I wrote some code taking a value with some data attribute like this: HTML <div class="evidence"> <div data-answer=".A,.B">1</div> <div data-answer=".B,.C">2</div> <div data-answer=".A,.C">3</div> </div> <div class="answer"> <div class="A">A</div> <div class="B">B</div> <div class="C">C</div> </div> Jquery $(document).ready(function(){ $('.evidence').on('click','div',function(){ $('.answer div').hide(); var ans = $(this).data('answer'); $(ans).show(); }) }) Here is the Demo Fiddle. It works but my question is if there is some way to improve my code avoiding write each classname in the answer column maybe showing the elements with eq() or some more efficient way. Thanks

Aucun commentaire:

Enregistrer un commentaire