0

I have a recurring function in my code, which consists of a set of thumbnails that, when clicked, plays a unique video. The video is in a div that's toggled. HTML is included for reference:

HTML

<ul class="testimonialthumbs">
<li id="t1"><img></li>
<li id="t2"><img></li>      
<li id="t3"><img></li>
<li id="t4"><img></li>
<li id="t5"><img></li>
</ul>
<hr>
<div class="testimonialdrop" id="v1">
    <iframe></iframe>
    <hr>
</div>

<div class="testimonialdrop" id="v2">
    <iframe></iframe>
    <hr>
</div>

<div class="testimonialdrop" id="v3">
    <iframe></iframe>
    <hr>
</div>

<div class="testimonialdrop" id="v4">
    <iframe></iframe>
    <hr>
</div>

<div class="testimonialdrop" id="v5">
    <iframe></iframe>
    <hr>
</div>

JS

$('#t1').click(function(){
  $('#v1').slideToggle('fast');
  $('#v2,#v3,#v4,#v5').hide();
  });

$('#t2').click(function(){
  $('#v2').slideToggle('fast');
  $('#v1,#v3,#v4,#v5').hide();
  });

$('#t3').click(function(){
  $('#v3').slideToggle('fast');
  $('#v1,#v2,#v4,#v5').hide();
  });

$('#t4').click(function(){
  $('#v4').slideToggle('fast');
  $('#v1,#v2,#v3,#v5').hide();
  });

$('#t5').click(function(){
  $('#v5').slideToggle('fast');
  $('#v1,#v2,#v3,#v4').hide();
  });

What is a more efficient way to write the JS above? The end result is that when #t(n) is clicked, #v(n) will expand and every other #v(n) will collapse (if expanded). The default display: on #v(n) is none.

4 Answers 4

2
// Whenever a list item in .testimonialthumbs is clicked...
$('.testimonialthumbs').on('click', 'li', function() {
    // Extract the number.
    var index = $(this).attr('id').substring(1);
    // Hide all the other divs.
    $('.testimonialdrop:not(#v' + index + ')').hide();
    // ...and slideToggle ours.
    $('#v' + index).slideToggle('fast');
});
Sign up to request clarification or add additional context in comments.

2 Comments

This works great! Compared to the answer from @ArunPJohny, is there a better practice? I noticed they're similar, so would the biggest difference be page-load time?
@KyleSimmonds: I guess, but a few bytes won't make much difference.
1

You can use StartsWith selector along with each() to iterate over your li:

$("[id^='t']").each(function(index) {
    var i = index + 1;
    $(this).click(function() {
        $('#v' + i).slideToggle('fast');
        $('#v' + i).siblings("[id^='v']").hide();
    })
});

Comments

0

try

$("ul.testimonialthumbs > li").click(function() {
  var id = $(this).attr("id"),
      matches = id.match(/\d+$/),
      idnum = matches[0];
  $("div.testimonialdrop").hide();
  $("div[id='v"+idnum+"']").slideToggle('fast');
});

Comments

0
$('.testimonialthumbs').on('click', 'li', function(){
    var id = $(this).attr('id');
    var el = $('#v' + id.substring(1)).slideToggle('fast');
    $('.testimonialdrop').not(el).hide()
})

Demo: Fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.