I cannot update a statically div via Jquery

Asked

Viewed 14 times

-2

Good afternoon, I wanted to keep static the text on the screen inside the div "msgemail", but it disappears soon after the validations. Can someone help me with my code ?

Thank you.

<!DOCTYPE html>
<html>
<head>
    <title>teste</title>
    <meta charset="UTF-8" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
    $('#form_contato').submit(function(){
        var er = new RegExp(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);
        var email = $('#txt_email').val();

        if( email == '' || !er.test(email) ) { alert('Email inválido'); return false; }
        var div = document.getElementById("msgemail");
        div.innerText = "Bem vindo " + email;
    });
});
</script>
</head>
<body>
    <div>
        <form action="" id="form_contato">
            <label>Email:</label>
            <input type="text" id="txt_email"/>
            <input type="submit" value="Enviar">
        </form>
    </div>
    <div id="msgemail">
    </div>
</body>
</html>

1 answer

0

Your form is being sent after the validations, you avoiding it, it will keep the message.

Place a false Return at the end of the function, it will remain on the same page.

 ...
    div.innerText = "Bem vindo " + email;
            
    return false; // adicione esta linha
    });
});
</script>
  • It worked, thank you Rogério.

Browser other questions tagged

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