How to do this cycle

var test1 = $('#m1 li img'); test1.hide(); var test2 = $('#m2 li img'); test2.hide(); var test3 = $('#m3 li img'); test3.hide(); var test4 = $('#m4 li img'); test4.hide(); 

Thank!

  • one
    It may be worthwhile to designate these elements as a general class and change it like this: $ ("# some_class"). Hide (); - ReinRaus
  • No, I made the data for an example, in real life a completely different picture - do4a
  • . $ (". some-class"). hide (); - istem
  • > no, the data is code made for example, in real life is a completely different picture and how is your other picture different? - DreamChild
  • there is more variable and they are very often consumed - do4a

1 answer 1

Maybe so?

 var selectors = ['m1', 'm2', 'm3'], len = selectors.length, elm; while(len--) { elm = $( selectors[len] + ' li img'); elm.hide(); // $( selectors[len] + ' li img').hide(); } 

Or give a real example in the studio.

  • but let it be so - do4a
  • And what the answer of the previous friend did not please? - istem
  • I didn’t minus it and didn’t delete it :), I don’t have it, it’s not your code, it didn’t fit, I just decided $ ('# slector') not to declare a separate variable, but use it several times - do4a