0

So I've been working on this web-project that demands a gallery with a slider underneath it. I've used this JavaScript so far to solve the problem in a forEach(element) function:

var divnumber = Array.from(element.parentNode.children).indexOf(element);

So the pagination changes by the index of the clicked element.

But since I need to make it responsive and the graphic designer demands something different in the mobile view I would need to get the number of the divs by using their class. Basically - the same array but different values.

Is there any way to tweak that line of code a bit to let it get the index of the element by its class instead of their parent? Here's the pen for more: https://codepen.io/ridonibishi/pen/BaNyBva

Thank you in advance!

  • 1
    How to get all elements with a specified class is a case for a search provider (Google, Yahoo, Bing, ...) but not SO – Andreas Feb 07 '20 at 14:48
  • I saw this question an our ago. None of the comments were helpful? – mplungjan Feb 07 '20 at 14:48
  • 2
    Or the SO search -> [How to Get Element By Class in JavaScript?](https://stackoverflow.com/questions/3808808/how-to-get-element-by-class-in-javascript) – Andreas Feb 07 '20 at 14:49
  • 2
    Funny enough none of the comments were helpful - and I think I just have to state that I'm still new to JavaScript every single time I ask something... unless I want ironic comments about how easy it is to fix it while not being helpful at all – fenrirmercenary Feb 07 '20 at 15:26
  • 1
    Because this is not how SO works (at least not in the past...) -> [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users/261593) - tl;dr: a lot – Andreas Feb 07 '20 at 15:47
  • 1
    @AkinOkegbile You've seen the (at the moment) only answer? – Andreas Feb 07 '20 at 16:36
  • 1
    Accept your answer as well @OP. – Akin Okegbile Feb 07 '20 at 16:38
  • @Andreas Sorry for the inconvenience! I try my best to use SO as my last option - which was the case this time since I don't understand JavaScript to its core as much as I would like to. I did not know that Array.from() could be used in the way as I intended to use it until I found out that I just could add getElementsbyClassName() since I did not find anything similar. Hopefully, it will work out better next time. Thanks for your effort! – fenrirmercenary Feb 10 '20 at 08:46

1 Answers1

1

Try using:

  var divnumber = Array.from(document.getElementsByClassName('class')).indexOf(element);

It works as intended.