1

I want to access the click event of an element on my HTML page.

So, in chrome, I right-clicked the element -> selected Inspect element and got the following

<button type="submit" class="btn btn-primary" id="ben-count-submit">Update</button>

I then went to a js page and wrote the following

$("#ben-count-submit").click(function () {      
        console.log("clicked on the update button");
    });

However, when I click on the button, I don't see any console log. It just takes me back to the home page of the web site and shows the url as the start page of the site appended with a '?' at the end as follows

http://localhost/myproject/start/?

How do I access the click event of this button? Where am I going wrong?

3
  • 1
    possible duplicate of Jquery: Prevent reloading page when pressing input type=submit Commented May 10, 2015 at 19:21
  • Sometimes you need reload the page, doing it you can change everyhthing. Commented May 10, 2015 at 19:24
  • If you are submiting form than use onsubmit function in form tag Commented May 10, 2015 at 19:25

1 Answer 1

1

Since your button is submit type, the button causes page refresh, you need to use preventDefault().

$("#ben-count-submit").click(function (event) {      
      event.preventDefault();
});
Sign up to request clarification or add additional context in comments.

2 Comments

thanks. It worked. I can't upvote since I don't have the reputation. Will come back when I have enough points.
@saltandwater do not wory, happy to help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.