Skip to content Skip to sidebar Skip to footer

Break Loop Based On Element Not Existing

I have built a cms that allows users to add up to 10 images into the slideshow, which all output in the front end in divs with ids of showcaseSlide with a number from 0-9 appended

Solution 1:

I wouldn't be so complex. I guess you have a class applied to all your slides div? If you do, use something like the following:

var slides = []
var divs = document.getElementsByClassName('slide-class')
for (var i = 0, l = divs.length; i < l; ++i) {
    slides.push("showcaseSlide" + i)
}

Btw, several comments about your code:

  • Don't use new Array(). Instead, use []. See here to understand why.
  • You didn't use the var keyword to declare your i variable, which means this variable is global. Global is evil.
  • document.write is evil.

I guess your count variable has some use later?

Solution 2:

You have DIVs numbered 0-9 but your loop runs 11 times.

Not actual code, but this explains it.

for(i=0; i<=10; i++){
  0 = 1st 
  1 = 2nd
  2 = 3rd
  3 = 4th
  4 = 5th
  5 = 6th
  6 = 7th
  7 = 8th
  8 = 9th
  9 = 10th
  10 = 11th
}

Post a Comment for "Break Loop Based On Element Not Existing"