Make html class appear and disappear by js

Asked

Viewed 628 times

2

I have a registration page, and when the data is entered wrong, a alert(); reporting the error, but wish instead of being Alert, it was an error message just below the fields. I put a span with an error message and with the class sr-only boostrap that makes what I wrote invisible, but how do I use js to make the class disappear? would be in place of Alert that is on echo.

HTML
<span id="cpf"></span>Cadastrar</button>
<span class="sr-only">Dados incorretos.</span>

PHP
    echo"<script type='text/javascript'>alert('Alguém com esses dados já se cadastrou. Tente novamente.');window.location.href='cadastro.php';</script>";
  • If you are not going to use an alert it would be better to do it directly in php, using a if whether or not to classify

  • Hello, could you mark the answer as correct? Thank you!

3 answers

2

document.getElementById("ID_DO_ELEMENTO").classList.remove("NOME_DA_CLASSE");

0

To add or remove classes directly by javascript you can do the following:

To add

document.getElementById("seu-id").classList.add("sua-classe");

To remove

document.getElementById("seu-id").classList.remove("sua-classe");

More information at this link

0

If you are using Jquery, you can use the following code:

To remove class:

$( "seu-id" ).removeClass( "sua-classe" );

To add class:

$( "seu-id" ).addClass( "sua-classe" );

More information Add Class and Remove Class

Browser other questions tagged

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