Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8846359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:57:49+00:00 2026-06-14T11:57:49+00:00

When a visitor clicks a button element, how can I change the URL? Example:

  • 0

When a visitor clicks a button element, how can I change the URL?

Example:

<form id="search">
  <input type="text" />
  <button onclick="javascript:alert('hi');">Alert Hi</button>
  <button onclick="javascript:window.location.href='http://www.google.com/'">Go to Google</button>
</form>

(Or see JSFiddle.)

For a quick demo page, I just need to send the visitor to a new URL when they click a button. I can execute JavaScript onclick (in the Alert Hi button), and the URL changes if I execute window.location.href='http://www.google.com/' from Firebug’s console. What do I need to do to change the URL on a button click?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:57:50+00:00Added an answer on June 14, 2026 at 11:57 am

    The problem is that the form is being submitted when you click on the button. Add type="button" so it doesn’t submit the form. You also don’t need javascript in the onclick attribute. See How to prevent buttons from submitting forms

    Example http://jsfiddle.net/E7UEe/1/ Notice that it won’t go to google.com because jsfiddle has disallowed it. Notice the error message Refused to display document because display forbidden by X-Frame-Options.

    <form id="search">
      <input type="text" />
      <button onclick="javascript:alert('hi');">Alert Hi</button>
      <button type="button" onclick="window.location.href='http://www.google.com/'">Go to Google</button>
    </form>​
    

    An alternative that works without JS is the appearance:button CSS directive http://jsfiddle.net/d6gWA/

    <a class="btn"> Link looking like button</a>​
    .btn {
        appearance: button;
        -moz-appearance: button;
        -webkit-appearance: button;
        -ms-appearance: button;
    }​
    

    Or you could always put the button in a link http://jsfiddle.net/d6gWA/2/

    <a class="btn" href="http://www.google.com" target="_blank"><button type="button">Link Button</button></a>​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jQuery colorpicker that the visitor can change and I am trying
How can I make a visitor's browser go fullscreen using JavaScript, in a way
How can I gather the visitor's time zone information? I need both: the time
I have a simple form based off a model called Visitor. I'd like to
when I input data are not yet available. button does not work but when
I am trying to trigger the Like action to a url by the visitor
I want to have an Adobe Edge animation, which plays after a visitor clicks
Guyz, I've a weird situation: I need to pre-compile input field when visitor opens
I've heard that there's a button you can hold to drag a selection of
I have a simple form which is like this: The HTML: <form action=post> <input

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.