1

I'm using jQuery to remove CSS class from my elements:

$('.input-group-addon').child($('.glyphicon.glyphicon-calendar').removeClass());    

Now on same action immediately I remove that class I want to add myClass for example so I tried:

$('.input-group-addon').child().addClass('myClass');

But this doesn't work, my element stays with class="" instead of class="myClass".

3
  • because you remove the class right?how can you add to a classless element? Commented Mar 3, 2016 at 10:06
  • Add your html code here Commented Mar 3, 2016 at 10:06
  • Replace .child() with .children(). There is no .child() method in jQuery. Commented Mar 3, 2016 at 10:17

3 Answers 3

2

You don't need to use children() at all:

$('.input-group-addon > .glyphicon.glyphicon-calendar').removeClass();

Add class back to all direct ancestors:

$('.input-group-addon > *').addClass('myClass');
Sign up to request clarification or add additional context in comments.

Comments

1

.child() is not a jQuery function replace it by .children() :

$('.input-group-addon').children().removeClass();
$('.input-group-addon').children().addClass('myClass');

Or without children() method :

$('.input-group-addon .glyphicon.glyphicon-calendar').removeClass();
$('.input-group-addon .glyphicon.glyphicon-calendar').addClass('myClass');

Hope this helps.

Comments

0

You need to specify the class you want to remove..

$('.input-group-addon').child($('.glyphicon.glyphicon-calendar').removeClass("className"));  

Currently, this

$('.input-group-addon').child($('.glyphicon.glyphicon-calendar').removeClass());  

removes everything.

Comments

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.