How to remove a class of multiple elements whose names obey a certain criterion

Asked

Viewed 118 times

3

I need to remove all classes starting with btn-*.

My script:

if (document.getElementById('contato_'+id).classList.contains('btn-danger') ){
    document.getElementById('contato_'+id).classList.remove('btn-danger');
    document.getElementById('contato_'+id).classList.add('btn-success');
}

The problem is that it only excludes a specific class.

  • You could select the classes by regular expression, rename them all to the same name and then remove.

  • You want to remove in a specific element or in all?

  • In my case this code is part of a function from which you take a specific element. Then I want to remove the class from the same element.

2 answers

5


Just go through all the classes of your element and remove those that satisfy your condition:

const button = document.querySelector('button')

for (let className of [...button.classList]) {
  console.log(`Testando a classe ${className}`)
  if (className.startsWith('btn-')) {
    button.classList.remove(className)
    console.log(`Removida a classe ${className}`)
  }
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<button type="button" class="btn btn-primary btn-lg">Large button</button>

  • What if the class starts with another name ? What can be done ?

  • @Scrapbench How so start with another name?

  • if (className.startsWith('btn-')) I was wondering if the class was "thing-to-bother btn-Primary btn-block" for example, this If would be validated in the same ?

  • 2

    Yes, because it considers each class separately. The condition is applied in coisa-para-chatear and then in btn-primary; as only the second satisfies the condition, only it will be removed.

  • Muchas Gracias

  • 1

    You’re the same faggot huh! D

Show 1 more comment

2

Insert in all your <button> class btn in prefix, example:

<button class="btn btn-sucess">

Thus the method getElementsByClassName(); will create an array of these Buttons;

var btns = document.getElementsByClassName("btn");


var len = btns.length;

for(var i = 0; i < len; i++){
   if(btns[i].className.includes('btn-')){
      btns[i].className = ''; //vazio se quiser retirar todas as classes;
      btns[i].className = 'nome_de_outra_class';
   }
}

In my case this code is part of a function from which you take a specific element. Then I want to remove the class from the same element.

function removerClass(elem,class_que_deseja_remover){
  if(elem.className == class_que_deseja_remover){
   // codigo a executar
  }
}
  • That function document.getElementById('contato_'+id).classList.remove('btn-danger') already removes specific class. I would like to delete classes with prefix " btn- "

  • search then with if(btns[i].className.includes('btn-')){ //fazer algo} edited the answer to include this

  • Care that includes search in string entire, which would return true for the class foobtn-yellow even without having the desired prefix.

  • give it some space before then resolves it ? ' btn-'&#I knew that I already knew that I did not want to know but to filter before

Browser other questions tagged

You are not signed in. Login or sign up in order to post.