0

I have a problem triggering a click event.

This is the code which should be called by the "click":

    $('#enable_autosave').click(function () {
        if ($(this).prop('checked')) {
            document.getElementById("autosave_information").innerHTML = "Aktiviert";
            document.getElementById("warning_information_all").style = "";
            IntervalId = setInterval(function Save_Pointer(){Save();}, 10000);
        } else {
            clearInterval(IntervalId);
            document.getElementById("autosave_information").innerHTML = "Deaktiviert";
            document.getElementById("warning_information_all").style = "visibility:hidden";
        }
    });

The HTML code:

<input type="checkbox" id="enable_autosave">Test</input>

And this is the code which should trigger the checkbox' click-event after loading the page:

$('#enable_autosave').trigger('click');

The checkbox gets checked but the click-event isn't fired. I also tried .click() or .onclick() or $('#enable_autosave').attr('checked', true)[0].onclick();

But they all show the same result, the checkbox gets checked but not click-event.

3
  • Does it work for you when you manually click on the checkbox Commented Jan 11, 2016 at 12:43
  • i hope the order of the attaching the event and then triggering is maintain... can you share the js actual code.. Commented Jan 11, 2016 at 13:21
  • I added the code in the main post. Commented Jan 12, 2016 at 0:21

2 Answers 2

1

This is working for me (with EventListener):

<input type="checkbox" id="enable_autosave">Test</input>    
<script>
         window.onload = function() {
            'use strict';
             var foo = function() {
                var test =  document.getElementById("enable_autosave").checked;

                if(!test){
                    console.log("no");
                }
                else{
                    console.log("yes");
                }

            }
            foo();

            document.getElementById("enable_autosave").addEventListener('input', foo);
            document.getElementById("enable_autosave").addEventListener('change', foo);

        }
    </script>
Sign up to request clarification or add additional context in comments.

Comments

0

If you want alert after checkbox, do something like this:

<input type="checkbox" id="enable_autosave" onchange = 'alert("HI")'>Test</input>

2 Comments

The real code in the function is longer but not interesting for this problem. The alert should just show, that I get no event fired.
Ok, try this: $("input:checkbox").bind("change click", function () { // do something });

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.