Modal Form Login appears once and disappears at the same time

Asked

Viewed 335 times

0

I have a problem here with this modal form. JS works fine, but the modal window appears and quickly disappears. How to solve?

<a href="" id="signin"><li>Entrar</li></a></a>
                </ul>
            </div>
        </nav>
    </div>
</header>
<!--modal-->
<div class="modal">
    <form class="modal-content animate">
        <div class="imgcontainer">
            <span class="close" title="sair">&times;</span>
            <img src="img/users.png" alt="users" class="avatar">
        </div>
        <div class="container">
            <label for="uname"><b>E-mail</b></label>
            <input type="text" name="uname" placeholder="Entre com o seu e-mail" required>
            <label for="psw"><b>Senha</b></label>
            <input type="password" name="psw" placeholder="insira sua senha">
            <button type="submit">Entrar</button>
            <label>
                <input type="checkbox" name="lembrar" checked="checked">Lembre-ne
            </label>
        </div>
        <div class="container"style="background-color: #f1f1f1">
            <button type="button"  onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancelar</button>
            <span class="psw">Esqueceu a <a href="">senha?</a></span>
        </div>
    </form>

<script>
    document.getElementById("signin").addEventListener("click", function(){
    document.querySelector('.modal').style.display = "block";
});

document.querySelector(".close").addEventListener("click", function(){
    document.querySelector('.modal').style.display = "none";
});
</script>
  • Thank you! Absolutely !

2 answers

1

First, it makes no sense for you to put an element <li> within an element <a>:

<a href="" id="signin"><li>Entrar</li></a>

This is not allowed by W3C and WHATWG standards. The correct would be to reverse the elements:

<li><a href="" id="signin">Entrar</a></li>

Second, the problem of briefly displaying the modal is due to your "log in" link. As you do not set a value in href, by default, it will point to the URL itself and when clicked, the page is reloaded. Your options are:

  1. Define the value of href as href="#!", so the browser will try to redirect the user to the element whose id="!" and, as it probably will not exist, naca will happen.

  2. Define the value of href as href="javascript:void()", having the same effect as the previous item; or

  3. Use the preventDefault() of the event click in its element:

    document.getElementById("signin").addEventListener("click", function(event){
        document.querySelector('.modal').style.display = "block";
        event.preventDefault();
    });
    

This way, you inform the user that you do not want the normal behavior of clicking an anchor and the user will not be redirected.

Additional readings:

-1

I was in real trouble. In this case I had a button that triggered the modal. Only before the button had a link that covered the button ai did not pick, take the link and C left the button see how it was.inserir a descrição da imagem aqui

Browser other questions tagged

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