30

I have this class called .m-active that is used multiple times throughout my HTML.

Basically what I want to do is remove all instances of that class when a user clicks on an image (which does not have the m-active class) and add the m-active class to that image.

For instance in a Backgrid row you might have a click handler as follows:

"click": function () {
    this.$el.addClass('m-active');
}

But you also want to remove that class from any rows to which it was previously added, so that only one row at a time has the .m-active class

Does anyone know how this can be done in javascript/jquery?

Dexygen
  • 12,287
  • 13
  • 80
  • 147
jdc987
  • 743
  • 3
  • 10
  • 20
  • Yes, I have tried removeClass but I could not get it to do what I want. – jdc987 Jul 15 '13 at 19:48
  • possible duplicate of [.removeClass from all elements (versus a single defined element)](http://stackoverflow.com/questions/6899888/removeclass-from-all-elements-versus-a-single-defined-element) – Mottie Jul 15 '13 at 19:49
  • The second part would be `$(this).addClass('m-active');` – f p Jul 15 '13 at 20:13

5 Answers5

62

With jQuery:

$('.m-active').removeClass('m-active');

Explanation:

  • Calling $('.m-active') selects all elements from the document that contain class m-active
  • Whatever you chain after this selector gets applied to all selected elements
  • Chaining the call with removeClass('m-active') removes class m-active from all of the selected elements

For documentation on this specific method, see: http://api.jquery.com/removeClass/

Getting grasp of the whole selector thing with jQuery is challenging at first, but once you get it, you see everything in very different light. I encourage you to take a look into some good jQuery tutorials. I personally recommend checking out Codeacademy's jQuery track: http://www.codecademy.com/tracks/jquery

jsalonen
  • 29,593
  • 15
  • 91
  • 109
  • Will this remove all of the instances of m-active that I have throughout my whole HTML file? – jdc987 Jul 15 '13 at 19:48
  • Yes in fact it does. See the explanation I added! – jsalonen Jul 15 '13 at 19:50
  • Great. This is just what I needed and couldn't find when searching. Thank you for explaining it as well (I am new to jquery/javascript/programming in general so this helped me)! – jdc987 Jul 15 '13 at 19:53
8

all answers point to remove the class from the DOM element. But if you are asking to remove the element itself you can user .remove() jquery method

$('.m-active').remove();

JQuery Remove Docs

Coding Enthusiast
  • 3,865
  • 1
  • 27
  • 50
Alireza
  • 5,444
  • 9
  • 38
  • 50
3

In plain JavaScript (no jquery):

for (elem of document.getElementsByClassName("m-active")) {
    elem.classList.remove("m-active");
}
AldaronLau
  • 1,044
  • 11
  • 15
0

Jquery-:

$("class").removeClass("your class");

javascript-: Set the class name to nothing when you want to remove class in javascript!!!

document.getElementById("your id").className = "";

or

element.classList.remove("class name");
HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87
0

Specifically addressing the code block added to strengthen the quality of the question, and borrowing from jsalonen:

"click": function () {
    $('.m-active').removeClass('m-active');
    this.$el.addClass('m-active');
}
Dexygen
  • 12,287
  • 13
  • 80
  • 147