sleekdev
sleekdev

Reputation: 229

Remove specific class when button/dropdown click

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.

Upvotes: 1

Views: 133

Answers (3)

Shaunak D
Shaunak D

Reputation: 20636

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')
});

Upvotes: 2

AmmarCSE
AmmarCSE

Reputation: 30557

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>

Upvotes: 1

kaushik dey
kaushik dey

Reputation: 102

jQuery('button.btn').click(function (e) {
        if (jQuery(this).find('.help-text').length) {
            jQuery('.help-text').removeClass('help-text');
        }
    });

Upvotes: 1

Related Questions