Help // Shortening Javascript
Author
Discussion

grumbledoak

32,571 posts

264 months

Wednesday 16th January 2019
quotequote all
You could generate the strings.

SlidingSideways

1,345 posts

263 months

Wednesday 16th January 2019
quotequote all
A loop would be the obvious first choice.

Assuming that's JQuery (and I'm a bit rusty here), can you give the elements in each group a different class and select and hide/show based on that?

durbster

12,141 posts

253 months

Wednesday 16th January 2019
quotequote all
SlidingSideways said:
A loop would be the obvious first choice.

Assuming that's JQuery (and I'm a bit rusty here), can you give the elements in each group a different class and select and hide/show based on that?
Yep, I agree. That'd be easier and cleaner. Give each element you want to be hidden or shown together the same class, then you can just select that class with jquery e.g.

 <div id="groupxxfoodyy" class="thingy"></div>
<div id="groupxxfoodyy" class="opposite-thingy"></div>

...

$('.thingy').show();
$('.opposite-thingy).hide();


Then all you have to do is select that class:

But if it helps for future reference, here's an idea of how you can use loops to do things like this:
https://codepen.io/kdurber/pen/WLWJKb?editors=0011

There's a handy function in there for always returning two digit numbers too. smile