newtphp newtphp - 6 months ago 18
jQuery Question

In JQuery how can I select all buttons which id start with "aaa" that have a class "class_name"?

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");

Answer

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.