- This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
Viewing 6 posts - 1 through 6 (of 6 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
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
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.
$('.commonbtnClass').click(function{
$(this).parent().addClass('newchangeClass');
//or
$(this).closest('CommonDivClass').addClass('newchageClass');
});
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.
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.
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();
@Senff work’s perfectly. Cant believe it was that simple, thank you.