All Posts by Email, Once a Week

Media Temple logo

CSS-Tricks is brought to you in part by Media Temple, the web hosting that we both use and recommend.

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

    This reply has been reported for inappropriate content.

    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.

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed