Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6347215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:10:16+00:00 2026-05-24T21:10:16+00:00

I have an input box that takes a number that gets verified in function

  • 0

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 AND 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:

<input type="text" value="" name="options[1]" id="options_1" onkeypress="return disableEnterKey(event)">

Event.observe($('options_1'), 'change', function(e){ 
   opConfig.reloadPrice(); 
}); 
Event.observe($('options_1'),'keypress', function(e){ 
  if (e.keyCode == Event.KEY_RETURN)  
    opConfig.reloadPrice(); 
});

I’ve tried several different solutions (I’ve tried ‘blur’ event, putting one observer in the other… )

I’m looking for a solution similar to this one
jQuery-Two events at the same time

Thanks a million



EDIT-Solved:

Thanks to katspaugh I solved this. There were few things that were not clear to me, one of them is: ‘keypress’ event is observed before ‘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 🙂 ).


Anyway, I change the code a bit that it suits me:

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;  
}); 

Cheers,

jazkat

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:10:18+00:00Added an answer on May 24, 2026 at 9:10 pm

    Save the current value of the input field in an upper-scope variable (up-value).

    If the value changes in one of the events, fire the handler and update the upvalue:

    var oldVal;
    
    $('options_1').observe(
      'change', function () {
          var val = this.value;
          if (oldVal !== val) {
              oldVal = val;
              opConfig.reloadPrice();
          }
      }
    ).observe(
        'keypress', function(e) { 
            oldVal = this.value;
            if (e.keyCode == Event.KEY_RETURN) {
                opConfig.reloadPrice();
            }
        }
    );
    

    Also, you’d better remove the inline listener in HTML (and never add them again):

    <input type="text" name="options[1]" id="options_1" />
    

    Demo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an input box that takes values from 0-100. I want to prevent
I have a page with an input box, and a function that processes the
In my ASP.NET page, I have an input box that has to have the
i have one form which have some input box and some select box. i
We have a command line exe that takes input from a text file and
I have a seperate JFrame where there is a text box (jTextArea) that takes
I have a device that takes low current 3-12v input signal to do it's
i wrote a function that takes a string as input. function ajaxLoad(targetBox, loadUrl) {
I have an input box that I don't want postback to occur on someone
I have small script that takes the value from a text input and needs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.