ether ether - 2 months ago 10x
Javascript Question

jQuery: If class exists, remove it, then add it to new element

I'm working on an accordion style video section. I'm currently trying to check if a class exists on click. I want it to only open 1 section at a time, so I need to

  1. Check if the class exists on click

  2. If it does, remove class

  3. Add class to the clicked element

I tried doing this with just
but am not 100% sure on what I'm missing.

Add Class


Toggle Class


This is the basic HTML set up

<div class="contents-row">
<div class="content-option press">
<div class="class-section-title">test1</div>
<div class="drop">
test 1

A full version of the drop down in on JSFiddle.

Thanks for the help!


First remove content-open class from all elements having content-open class. Then add the class to clicked element like following.