How to make a button link to a page if condition is true?

Solution for How to make a button link to a page if condition is true?
is Given Below:

I’m trying to make a login page, but everytime I press the login button it always redirect to my site’s dashboard, even if the password is incorrect.

Can I do something like this?

if password = helloworld :
    <a href=“”>LOG IN<a>

*** I’m a starter in HTML, so I copied a login source code from a random repo in github. Here’s the source code :

Thanks in advance 🙂

You are going to need to use a <script> tag which will allow you to run JavaScript on your page.

Something like this should work:


Some clarification based on OP’s additional comment, this code should go in the HTML file and replace your <button type="button" onclick="window.location.href=""">LOGIN</button>.

<button type="button" onclick="logIn()">LOGIN</button>

  // Note: This is only hard coded for example purposes, it should probably come from user input
  let password = 'helloworld';

  function logIn() {
    if (password === 'helloworld') {
      window.location = "";

While that should work just fine for a starter project, it is extremely insecure and you should consider the password used, as something visible to anyone who visits your site. (You should never hard code passwords like this for anything that you expect to be even remotely secure)

If you are looking to implement a secure auth system you are likely going to want to look for a tutorial online teaching you how to either implement your own system, or use a pre-existing auth service.