Required checkbox with javascript to click link

Asked

Viewed 1,201 times

1

Good evening, everyone!

I am making a website for a college job, and what I want to do is the following: I have a simple HTML menu whose options are "View Profile", "Edit Profile" and "Remove Profile". In the latter, there is a checkbox saying that the user is sure of what he wants. To remove the profile the person must first have selected it (similar to those "I read and accept the terms"). If you click "Remove Profile" without first selecting the checkbox, an error message should appear.

My doubt: How to build a code that requires the user to click the checkbox first to remove the profile and be directed to another page, and in case it doesn’t display an error message?

See the code:

       <ul id="menuperfil">
           <li><a href="perfilview.html">Visualizar Perfil</a></li>     
           <li><a href="perfiledit.html">Editar Perfil</a></li>
           <li><a href="perfildelete.html">Remover Perfil</a>
             <form>
        <input type="checkbox" name="remover">Tenho certeza de que desejo remover o perfil.
             </form></li>
       </ul>

With CSS it looks like this:

inserir a descrição da imagem aqui

What’s up guys? Any suggestions??

Thank you for your attention!

  • Welcome to Stack Overflow, Felipe! Please use a minute of your time to do the tour, and better know how the site works! There is more information on Help center. And see if that answer helps you.

1 answer

1


<a href="#" onclick="fcnConfirma();">Remover Perfil</a>
<input type="checkbox" id="remover" name="remover">Tenho certeza de que desejo remover o perfil.


<script type="text/javascript">
    function fcnConfirma() {
        if (!document.getElementById('remover').checked) {
            alert("Marque a opção:\nTenho certeza de que desejo remover o perfil.");
            return;
        }else{
            window.open("perfildelete.html","_self")
            // window.open("perfildelete.html","_blank")
        }
    }
</script>
  • It worked!! Thank you very much! ^^

  • @Felipe, if our friend Lelecko solved your problem don’t forget to set the answer as settled! =D

Browser other questions tagged

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