Nested event listeners?
Nested event listeners?
I'm new to jQuery, so I'm hoping y'all can give me some guidance here. I've narrowed down my problem, and now I just need someone to show me the better way.
Apparently in jQuery it's a bad idea to have nested event listeners. It makes sense to me: Say I have a button that toggles between two states. In the first state, another button is activated and can be clicked (another event listener there). In the second state, that button cannot be clicked.
But when I implement this, here's the result: The first time it works great. Then when I toggle out and back in, it runs the inner listener twice when clicked. And then the third time it runs three times. And so on, and so forth.
Just to be sure I'm making myself clear, here's a sample bit of code I put together to illustrate this:
- $(function() {
- $('#Button1').toggle(function() {
- $('#Button1').attr("value", "yes you may");
- $('#Button2').removeAttr("disabled");
- $('#Button2').click(function() {
- var output = $('div');
- output.append("! ");
- });
- }, function() {
- $('#Button1').attr("value", "no you may not");
- $('#Button2').attr("disabled", "disabled");
- });
- });
So my question is, how can I make this do what I want it to, that is, run the instructions inside the inner event listener only once every time?
Thanks in advance!