1

I want to remove specific class ("help-text"). when user click on button specific class will be removed.

Here is my code

<div class="dropdown dropdown-lg">
    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
        <img src="img/find-match.png"/>
        <span class="help-text">Click Here</span>
    </button>
</div>

<script>
jQuery(document).click(function (e) {
    if (!jQuery(e.target).hasClass('dropdown-lg')) {
        jQuery('.dropdown-lg').removeClass('help-text');
    }
});
</script>

Kindly advice me any solution.

3 Answers 3

2

The span with help-text, is inside the button. So you can use,

$(e.target).find('.help-text').removeClass('help-text')

or

$('.help-text',e.target).removeClass('help-text')

Also, instead of handling click on document I would suggest,,

$('button.dropdown-toggle').click(...

Fiddle

$('button.dropdown-toggle').click(function(){
    $(this).find('.help-text').toggleClass('help-text')
});
Sign up to request clarification or add additional context in comments.

2 Comments

Do you want to remove the class, or remove the element? This works ay my end. Check this jsfiddle.net/BqKNV/401
when user click on button class is removed but when user again click on button for hide div then class is not set back
1
jQuery('button.btn').click(function (e) {
        if (jQuery(this).find('.help-text').length) {
            jQuery('.help-text').removeClass('help-text');
        }
    });

1 Comment

when user click on button class is removed but when user again click on button for hide div then class is not set back. I only want to removed class/element on click.
1

SInce .help-text is a child of the button, use find()

jQuery('.dropdown-lg').find('.help-text').removeClass('help-text');

jQuery('.dropdown-lg').click(function(e) {
  jQuery(this).find('.help-text').removeClass('help-text');
});
.help-text {
  background: green;
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="dropdown dropdown-lg">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
    <img src="img/find-match.png" /><span class="help-text">Click Here</span>
  </button>
</div>

2 Comments

OH, sorry, had a typo, changed removeClass to find for the first call
@dev, see the working example in my answer. It is in the code snippet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.