Home > Net >  HTML Button wont do anything when clicked
HTML Button wont do anything when clicked

Time:10-06

I am making my website and I have my header buttons wont do anything. The main problems are on lines 12 to 15. Here is the code:

<!DOCTYPE html>
<html>
    <head>

        <script src="https://kit.fontawesome.com/6feb1dab38.js" crossorigin="anonymous"></script>
        <link rel="stylesheet" href="resources/css/stylesheet.css">
        <title>Home</title>
    </head>
    <header>
    </header>
    <body>
        <div class="context">
            <button class="headerButton" type="button">About Me</button>
            <button class="headerButton" type="button">Links</button>
            <button class="headerButton" type="button" onclick="window.location.href='Clicked/Works'; console.log('it worked')">Works</button>
            <br>
            <script src="resources/js/app.js"></script>
        </div>
    
    
    <div class="area" >
                <ul class="circles">
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                        <li></li>
                </ul>
        </div >
        <script src="resources/js/app.js"></script>
    </body>
</html>

I think it has something to do with the buttons being inside the context . Please let me know any answers you may have.

CodePudding user response:

You are assigning a value to location.href, causing the page to redirect.

Change your button to:

 <button class="headerButton" type="button" onclick="console.log('it worked')">Works</button>

CodePudding user response:

It is not really clear what you are trying to achieve. If you want to hook an event handler to the button then you need to write the following in your js code:

HTML:

<button id="my-button">
  CLICK ME!
</button>

JS:

const button = document.querySelector('#my-button');
button.addEventListener('click', () => {
    button.innerHTML = 'YOU CLICKED ME!';
    // ... or perform whatever action you would like...
})

Here's an example code of this:

https://stackblitz.com/edit/js-kt8cab?file=index.js

  • Related