javascript - How to "remove class" with jQuery? -


i have following code show , hide div if want click on image can add class if want remove clicking image open again. can explain me how works?

https://jsfiddle.net/bmhv3edw/

$(document).ready(function() {     function close_answer_section() {         $('.question-text').removeclass('active');         $('.answer-section-content').slideup(300).removeclass('open');     }      $('.question-text').click(function(e) {         var currentattrvalue = $(this).attr('href');          if($(e.target).is('.active')) {             close_answer_section();         }else {             close_answer_section();              $(this).addclass('active');             $('.questions ' + currentattrvalue).slidedown(300).addclass('open');          }          e.preventdefault();     }); }); 

you can see when clicked on img, e.target <img>, don't have class .active, it'll trigger false part.

solution, change

if($(e.target).is('.active')) { 

to

if($(this).is('.active')) { 

see jsfiddle


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -