title: Observer events in JS Prototype - two events at the same time
body: <p>I have an input box that takes a number that gets verified in function opConfig.reloadPrice(). I set 2 observers one the same element, on 'change' it executes the code when I click outside the box or use tab-if the change has been made. On 'keypress' it checks if the return key was pressed then executes the code <strong>AND</strong> it also triggers the 'change' observer!! So in this case the function opConfig.reloadPrice()  gets executed twice (and I end up with two identical alert boxes).
  The code that I have now:</p>
  
  <pre><code>&lt;input type="text" value="" name="options[1]" id="options_1" onkeypress="return disableEnterKey(event)"&gt;
  
  Event.observe($('options_1'), 'change', function(e){ 
     opConfig.reloadPrice(); 
  }); 
  Event.observe($('options_1'),'keypress', function(e){ 
    if (e.keyCode == Event.KEY_RETURN)  
      opConfig.reloadPrice(); 
  });
  </code></pre>
  
  <p>I've tried several different solutions (I've tried 'blur' event, putting one observer in the other... )<BR>
  I'm looking for a solution similar to this one 
  <em><a href="https://stackoverflow.com/questions/6581088/two-events-at-the-same-time">jQuery-Two events at the same time</a></em></p>
  
  <p>Thanks a million
  <BR><BR>
  EDIT-Solved:<BR>
  Thanks to <strong>katspaugh</strong> I solved this. There were few things that were not clear to me, one of them is:  'keypress' event is observed <em>before</em> 'change' event, which is why you can set variables in one observer and check them later in another (this is also why it took me a while to understand katspaughs code :-)  ).<BR><BR>
  Anyway, I change the code a bit that it suits me:</p>
  
  <pre><code>var enterPressed = 0;  
  
  Event.observe($('options_1'), 'change', function(){ 
    if (enterPressed !== 1) {    
      opConfig.reloadPrice();  
    }
    enterPressed = 0;
    });
  
  Event.observe($('options_1'), 'keypress', function(e){  
  if ((e.keyCode == Event.KEY_RETURN) || (e.keyCode == Event.KEY_TAB)) {
    enterPressed = 1;
    opConfig.reloadPrice();   
  }
  else 
    enterPressed = 0;  
  }); 
  </code></pre>
  
  <p>Cheers,<BR>
  jazkat</p>