Forums

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

Home Forums JavaScript select parent element of element being clicked

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #154428
    Anonymous
    Inactive

    I have multiple divs with the same class and same content. I have a button on each div with the same class. I want to be able to click that button, but not effect all elements. Only effect the parent div of the button being clicked. Preferably with jquery.

    #154444
    gowda24
    Participant
    $('.commonbtnClass').click(function{
        $(this).parent().addClass('newchangeClass');    
    
    //or
    
      $(this).closest('CommonDivClass').addClass('newchageClass');
    
    });
    
    #154473
    Anonymous
    Inactive

    Sorry i made a mistake explaining. i don’t want to effect the parent div the button is inside of. I want to effect a div that is within the same div the button is in.

    #154518
    Anonymous
    Inactive

    I created a pen to better visualize what i mean.
    http://codepen.io/Jarolin/pen/IqyrH

    When i click “click” all panels expand. I want only the panel with the corresponding “click” button to expand and shrink.

    #154525
    Senff
    Participant

    I’d agree with @justdan that a full-on accordion might be a better option here, however to quickly fix what you have now, replace:

    $(".article_container").stop().slideToggle();
    

    with:

    $(this).next(".article_container").stop().slideToggle();
    
    #154530
    Anonymous
    Inactive

    @Senff work’s perfectly. Cant believe it was that simple, thank you.

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