Grow your CSS skills. Land your dream job.

Index in array issue

  • # October 13, 2013 at 2:10 pm

    Hi to everyone. My situation: I have array with 200 elements var block = document.getElementsByClassName( 'block' ); 4 of elements have additional class box. How can I set to different variables index of elements with class box. Something like:

    var first = 6 ( index of element with class box);
    var second = 67 ( index of element with class box);
    var third = 112 ( index of element with class box);
    var fourth = 189 ( index of element with class box); 
    

    Thanks for any help.

    # October 14, 2013 at 10:48 am

    Try this:

    var blocks = document.getElementsByClassName( 'block' );
    var boxes = [];
    
    for(var i = 0; i < blocks.length; i++){
      if(a[i].className.indexOf('box') !== -1){
        boxes.push(blocks[i]) '
      }
    }
    
    # October 14, 2013 at 11:21 am

    Thanks @jamygolden. Your solution is very helpful, but my intention was recieve indexes, like boxes.push(i). Because of my english I couldn’t properly explain problem. Thanks again.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".