If you give data-toggle attributes for your elements as follows:
<span id="paylas_" data-toggle="#pyls"></span>
then you can combine the selector and use .data("toggle") to get the corresponding selector:
$(document).ready(function() { var toggles = {}; $('#paylas_, #uyari_, #dahada_').click(function() { var cached = this.id in toggles; if(!cached) { toggles[this.id] = $(this).data("toggle"); } var toggle = toggles[this.id]; $('#uyri, #dhda, #pyls').not(toggle).slideUp(); $(toggle).slideToggle('normal'); }); });
If you use data-toggle only for these elements, you can also do:
$('[data-toggle]').click(function() {
I cannot compose something from identifiers, but if there is a certain structure, you could also generalize the second list of selectors.
source share