1

How can I select all buttons which id starts with "aaa" that have a class which name is class_name1?

Summary:

  • select all element which id start with "aaa"
  • inside this group select all button which have a class called class_name1
  • remove this class and add a new class class_name2.

The last step should be:

$('#id').removeClass("class_name1").addClass("class_name2");
Artur Filipiak
  • 9,027
  • 4
  • 30
  • 56
newtphp
  • 245
  • 1
  • 4
  • 13

2 Answers2

3

See jQuery ID starts with

$('button[id^="aaa"].class_name1').removeClass("class_name1").addClass("class_name2");

button[id^="aaa"] looks for buttons with ids starting with 'aaa'. .class_name1 limits those to elements with the class class_name1.

Community
  • 1
  • 1
neilsimp1
  • 1,242
  • 1
  • 11
  • 25
1

In JQuery, there is a class selector. For example, if you wanted all elements with class = class1, you would use $('.class1').each(function(){...});

Within this each() function, you could use the JQuery wildcard selector to get all elements with an id starting with 'aaa':

if($('[id^=aaa]')){ $('.class1').addClass('class2'); $('.class1').removeClass('class1'); } });

I believe you will need to take care of those add and remove methods on separate lines in order for this to work.

Hope this helps.

R. Hilyer
  • 13
  • 4