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 8059025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:32:14+00:00 2026-06-05T09:32:14+00:00

I want to assign the function below with a button, but by some reason

  • 0

I want to assign the function below with a button, but by some reason the ‘location.href’ part in the function doen’t work. If I change from a button to a div it works, so the problem seems to be with the button.

How come it doesn’t work and is there a way of solving it (I really want to use a button)?

<button id="back">Cancel</button> // Doesn't work
<div id="back">Cancel</div> // Works

    $('#back').click(function(){
        var imageId = $('#id').attr('href');
        var id = imageId.split('/');
        location.href = '/blog/' + id[3];
    });
  • 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-05T09:32:16+00:00Added an answer on June 5, 2026 at 9:32 am

    Your problem is not the button, your problem is the fact that when you bind a click to an element with a default behavior, like a link or button, the browser executes the script but first starts following the default behavior.

    You either use return false; to prevent the default behavior from triggering, and also prevent other actions associated with the element to bubble up:

    See this working Fiddle Example!

    $('#back').click(function(){
        return false;  // prevent the regular click action for the element
        alert('bubu'); // this will not run
    });
    

    or you use .preventDefault(); to only prevent the default behavior but perform other actions associated with the element:

    See this working Fiddle Example!

    $('#back').click(function(event){
        event.preventDefault(); // prevent the regular click action for the element
        alert('bubu'); // this will run
    });
    

    There’s a good article on the subject here!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know How JQuery assign function name to $ ?
What I want to do is following. Inside a function, I need to assign
i want to assign a class to an a href tag if this his
I have a js function I want to assign a variable to a variable
I want to do some operation in a particular cell using some function. Here
I want to assign a function to several div (show + i) at the
I want to reproduce a get style function from C++ in a C program.
Let's say I have a function def x(): print(20) Now I want to assign
I want to check in client side(jQuery) whether return data from a PHP function
Consider the following function, which does not work in Python, but I will use

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.