0

This is my HTML div tag

<div class="modal modal-success"  id="myModal">

I want to add or remove class dynamically using jQuery. I want to match only the the model-* ( eg model-success , modal-danger etc ) part and remove that part whenever want

For example

<div class="modal "  id="myModal">
$('#myModal').addClass("modal-"+messageType);
//message type will be success , warning

After adding that I want to remove that newly added part dynamically

 <div class="modal modal-success " id="myModal">

I want to keep the modal part as it is and remove the modal-sucess part

How can I do this using jQuery?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
namilaR
  • 5
  • 5

1 Answers1

0

If you use .removeClass(); with a parameter of the class you wish to remove it will only remove the class specified, eg: .removeClass('modal-' + messageType); will remove '.modal-success', '.modal-warning' etc. depending on the variable value.

If you use .removeClass(); with no parameters then it will remove all classes.

See the docs for a more comprehensive explanation https://api.jquery.com/removeclass/

Scott Harrison
  • 393
  • 4
  • 17
  • Thank you ! can i use .removeClass('modal-*') to remove a class starting from modal- ? – namilaR Feb 12 '16 at 10:38
  • I don't think you can use wildcard selectors in the `.removeClass();` function. You may find some more information here: http://stackoverflow.com/questions/2644299/jquery-removeclass-wildcard – Scott Harrison Feb 12 '16 at 11:39