rigger12
rigger12

Reputation: 100

Prevent change event from firing on checkbox after page reload

Each time I refresh the page the change event is fired on the checkbox. How to I prevent this from happening?

I have a standard checkbox in the form:

<div class="toggle-icon"><input id="this-box" class="toggle-icons" type="checkbox">
    <label for="this-box"><img class="toggle-image" width="36px" height="36px" src="/somedirectory/"></label>
</div>

In the javascript I have the change handler for the checkbox:

    $(document).ready(function() {
      $('#this-box').change(function(e){
       //some actions I do not want to occur on page refresh
      });
    });

Appreciate the solutions people have posted, however there may be another issue with the code. Unfortunately cannot post greater-code-base here.

Upvotes: 0

Views: 804

Answers (1)

Victor Vieira
Victor Vieira

Reputation: 19

Create a click event, so its function will only be triggered when clicking.

$("#this-box").click(function () {
    alert("Handler for .click() called.");
});

Upvotes: 1

Related Questions