JavaScript On click add or remove class from div click remove class jquery,click remove class,click anywhere remove

I have a markup that looks like the following:

<div class="accordion is-open">
    <h3 class="accordion-header">
        HADER
        <span class="accordion-indicator"></span>
    </h3>
    <div class="accordion-content"></div>
</div>

<div class="accordion is-open">
    <h3 class="accordion-header">
        HADER
        <span class="accordion-indicator"></span>
    </h3>
    <div class="accordion-content"></div>
</div> 

<div class="accordion">
    <h3 class="accordion-header">
        HADER
        <span class="accordion-indicator"></span>
    </h3>
    <div class="accordion-content"></div>
</div> 

What I am trying to do is to use jquery to add or remove class .is-opened to div with class .accordion when class .accordion-header

My problem is that some of the divs already contain class .is-opened and some don't. I need to be able to click and open (add class .is-opened) only that specific div that was clicked.

Here is my jQuery:

function accordionOpener(obj) {
    var accordionHeader = obj.find('.accordion-header');
    var accordionBody = obj.find('.accordion');
    accordionHeader.on('click',function(){
        $(accordionBody).toggleClass('is-open');
    });
}
new accordionOpener($(this));

I am not a big pro in jQuery, JavaScript so speak to me as if I am 10 yeas old.

Answer:1

Your logic isn't quite correct. To fix this attach a click event handler directly to the .accordion-header class. From there you can use DOM traversal to find the related .accordion element and toggle the class. Try this:

$(function() {
  $('.accordion-header').click(function() {
    $(this).closest('.accordion').toggleClass('is-open');
  });
});
.is-open { color: #C00; } /* this is only to see the class being added/removed */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="accordion is-open">
  <h3 class="accordion-header">
        HADER
        <span class="accordion-indicator"></span>
    </h3>
  <div class="accordion-content"></div>
</div>

<div class="accordion is-open">
  <h3 class="accordion-header">
        HADER
        <span class="accordion-indicator"></span>
    </h3>
  <div class="accordion-content"></div>
</div>

<div class="accordion">
  <h3 class="accordion-header">
        HADER
        <span class="accordion-indicator"></span>
    </h3>
  <div class="accordion-content"></div>
</div>
Answer:2

I'm using vuejs in my project and noticed that when a page is in loading state, I see something like this {{ user.id }} {{ user.name }} which is very annoying. And after the page is fully loaded, I ...

I'm using vuejs in my project and noticed that when a page is in loading state, I see something like this {{ user.id }} {{ user.name }} which is very annoying. And after the page is fully loaded, I ...

I have a rectangle on an SVG element. I then want to draw another rectangle over the whole SVG area except the rect I've already drawn. I thought masks might be a solution but they seem to act just ...

I have a rectangle on an SVG element. I then want to draw another rectangle over the whole SVG area except the rect I've already drawn. I thought masks might be a solution but they seem to act just ...

I'm using jQuery to create a simple addClass on hover. Hovering over a #science-panel-number div triggers a class of .active to be added to an #iphone-screen-number div. Here is my jQuery: $('#...

I'm using jQuery to create a simple addClass on hover. Hovering over a #science-panel-number div triggers a class of .active to be added to an #iphone-screen-number div. Here is my jQuery: $('#...

  1. number with jquery validate
  2. number with commas jquery
  3. format number with jquery
  4. random number with jquery
  5. number counter with jquery
  6. increment number with jquery
  7. number with decimal jquery
  8. add number with jquery
  9. increase number with jquery
  10. replace number with jquery
  11. number jquery
  12. number jquery plugin
  13. number jquery animation
  14. number jquery form validation
  15. number jquery selector
  16. number jquery check
  17. number counter animation with jquery
  18. validate phone number with jquery
  19. class with number jquery

Suppose I ensure that properties are deleted and objects are set to null after use in JavaScript prpgram. 1) Will this help me in better memory management? How do I verify this? 2) As far as I know,...

Suppose I ensure that properties are deleted and objects are set to null after use in JavaScript prpgram. 1) Will this help me in better memory management? How do I verify this? 2) As far as I know,...

  1. memory manager javascript