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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:43:24+00:00 2026-05-25T16:43:24+00:00

When binding to a click event for a checkbox input, the checkbox is already

  • 0

When binding to a click event for a checkbox input, the checkbox is already toggled by the time my event handler runs and, more oddly, the toggle is reversed after my event handler runs if I specify event.preventDefault();

<input id="foo" type="checkbox"/>

function clicked(evt) {
   alert(document.getElementById('foo').checked);
   evt.preventDefault();
}

document.getElementById('foo').addEventListener('click',clicked);

[tested in chrome and firefox]

JSFiddle for that code

The alert will respond “true” (or the opposite state of the checkbox pre-click). After you dismiss the alert, the checkbox toggles back.

So, I guess the questions are,

What is the actual default event being prevented? Am I wrong in assuming my event handler should be running before the state is changed? Is there a way to legitimately intercept a checkbox click?

And why in the world is the preventDefault causing the re-toggling the checkbox?

  • 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-05-25T16:43:24+00:00Added an answer on May 25, 2026 at 4:43 pm

    The actual default event being prevented is the click event. The misunderstanding probably occurs because you are thinking of the event as firing after the actual click has been fully processed (i.e. the checkbox has been toggled) while in reality the event model stipulates that the handler fires while the event is being processed.

    If it helps, another model I ‘ve found useful in explaining how it all works is the database transaction model: think of your event handler as being invoked as part of a transaction, inside which the checkbox has already been toggled.

    • If you read the state of the checkbox, you will find it toggled (the “write” has been sent to the database).
    • However, you can still decide to rollback the transaction (in which case the write is undone and the checkbox is toggled back to its original value).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a click event handler that looks like this: $('#show_existing_suggestions').click(function() But after some
I'm binding a click event to a div element, which is created after clicking
The following XAML produces a run-time Binding error when I click on an item
<DataGridTemplateColumn Header=IsAdmin> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox Name=IsAdminCheckBox IsChecked={Binding Path=IsAdmin} Click=IsAdmin_Click CommandParameter={Binding Path=Id}/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>
I am binding the click event to 4 href's (they have class=alink) using JQuery
Why isn't jQuery binding to the click event? When I click the selects, they
I can't seem to add a click event handler to the Dialog header/title. Something
I am binding a single click event to a large container using jQuery (event
I have the following code. The first attempt at binding to click event does
The checkbox data-binding value is not changing when i try to trigger the click

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.