Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Applying var and function to multiple divs with the same class

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #156941
    mdyorke
    Participant

    Hey All,

    I am new to JS and was wonder if someone could help me out with a problem I am having.

    On the page a I have multiple divs with the same class that I am targeting through this code.

    $(document).ready(function() {
    
            var portfolio_piece = $('.portfolio-piece-info-inner').height() / 2;
    
            $('.portfolio-piece-info-inner').css('margin-top', portfolio_piece);    
    
    
    });
    

    The problem I am having is that the variable is returning the height of the first .portfolio-piece-info-inner and applying it to the rest. What I would like it to do is find that variable of each .portfolio-piece-info-inner and apply it to that specific one.

    Any Ideas?

    Thanks!

    #156945
    Alen
    Participant

    http://codepen.io/anon/pen/stfBu

    When you use $('.thing') it essentially returns array, you would you first() to pluck the first item out of the array.

    Hope that helps

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.