How can I make a button redirect my page to another page?

user1464139 picture user1464139 · May 15, 2013 · Viewed 1.2M times · Source

I have been trying the following:

<form action="/home" class="inline">
    <button class="float-left submit-button" >Home</button>
</form>

It seems to work but it goes to the page "/home?"

Is there a better way for me to make a button inside a form make the page go to a new location?

Answer

mattytommo picture mattytommo · May 15, 2013

Just add an onclick event to the button:

<button onclick="location.href = 'www.yoursite.com';" id="myButton" class="float-left submit-button" >Home</button>

But you shouldn't really have it inline like that, instead, put it in a JS block and give the button an ID:

<button id="myButton" class="float-left submit-button" >Home</button>

<script type="text/javascript">
    document.getElementById("myButton").onclick = function () {
        location.href = "www.yoursite.com";
    };
</script>