admin管理员组

文章数量:1303451

I am try to remove the same class on multiple ids

Here's the code:

var test = ['#step-1','#step-2','#step-3'];

$(test).removeClass('active-step');

This is not working, my question is..what's the problem or is there a better way of doing this?

I am try to remove the same class on multiple ids

Here's the code:

var test = ['#step-1','#step-2','#step-3'];

$(test).removeClass('active-step');

This is not working, my question is..what's the problem or is there a better way of doing this?

Share Improve this question asked Mar 24, 2016 at 9:50 Satch3000Satch3000 49.4k90 gold badges224 silver badges349 bronze badges 3
  • $('[id^=step]').removeClass("active-step"); – light_ray Commented Mar 24, 2016 at 9:51
  • Shouldn't it just be one string? $('#step-1,#step-2,#step-3').... – paullb Commented Mar 24, 2016 at 9:54
  • Jquery selector acts the same as CSS selector – T-moty Commented Mar 24, 2016 at 10:13
Add a ment  | 

3 Answers 3

Reset to default 8

Try to frame that array as a multiple selector,

var test = ['#step-1','#step-2','#step-3'];
$(test.join()).removeClass('active-step');

As per the documentation, .join() will merge the elements of an array by using ma(,) if none supplied as its delimitter. So in our case, this signature would be useful in making a multiple selector.

separator : Optional. Specifies a string to separate each element of the array. The separator is converted to a string if necessary. If omitted, the array elements are separated with a ma. If separator is an empty string, all elements are joined without any characters in between them.

This is the correct jQuery syntax to select multiple ids:

$('#step-1, #step-2, #step-3').removeClass('active-step');

If you want to remove the classes of all elements whose id starts with step, then you can use starts ^ with attribute selector

$("[id^='step']").removeClass('active-step');

I thing your requirement is to remove the active calsses and add the active class to a particular element, then you can use like this too,

$(".active-step").removeClass('active-step');

本文标签: javascriptjQuery Remove same Class on Multiple Id39sStack Overflow