0

This is a general question from a newbie -

In javascript / (preferably) jQuery, how do I make a function that is triggered if (and only if) multiple events are satisfied?

More specifically, how to I trigger an event only after a (1.) button is clicked and (2.) an HTML tag is changed? (Documentation here: http://api.jquery.com/change/)

Filburt
  • 17,626
  • 12
  • 64
  • 115

2 Answers2

2

If you want to use promises:

$.when(
    $.Deferred(
        function(d) {
            $("#mybutton").click( 
                function() {d.resolve();}
            );
        }),
    $.Deferred(
        function(d) {
            $("#myhtml").change( 
                function() {d.resolve();}
            );
        })
).done( function() { alert("Both click and change happened"); } );

Although this will work only the first time.

f.cipriani
  • 3,357
  • 2
  • 26
  • 22
0

some thing like this

function (a, b, fn) {  
    $(a).click(function(){
        $(this).attr('data-state','clicked');
    })       

    $(b).click(function(){
        if ($(a).attr('data-state') == 'clicked') {
            fn(this);
            $(a).attr('data-state','reset');
        }
    })
}
kwarunek
  • 12,141
  • 4
  • 43
  • 48