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 7949847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:05:33+00:00 2026-06-04T02:05:33+00:00

Im trying to assign an onKeyUp event to all inputs within a form, using

  • 0

Im trying to assign an onKeyUp event to all inputs within a form, using closures. the array fields contains all the names of the fields that require the event assigned to them. The array ajaxFields contains the names of the fields (from the array fields) that requires ajax validation.

function createEvents(fields,ajaxFields) {
    for(var x=0;x<fields.length;x++) {

        $('input[name='+fields[x]+']').keyup(function(field) { 
        //assign an onKeyUp event
            return function() {
                //some code using variable 'field' and array 'ajaxFields'
        }(fields[x]));
    }
}

I would like the onKeyUp function to be executed a second after the user has finished typing in that field, insted of every time the key is up (onKeyUp). this would save up a lot of processing space, not to mention the ajax calls. So far im using this:

clearTimeout(timer);
timer = setTimeout('validate()' ,1000);

You might have noticed that the function validate() doesn’t exist, and thats because I dont know how to wrap the closures inside a named function, and im not even sure if I should…

So how do I do that?

EDIT: here is a current fiddle

  • 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-06-04T02:05:35+00:00Added an answer on June 4, 2026 at 2:05 am

    You can (and should) pass functions to setTimeout instead of strings.

    clearTimeout(timer);
    timer = setTimeout(function(){
        // your code here
    }, 1000);
    

    So, in your keyup, try something like this:

    $('input[name='+fields[x]+']').keyup(function(field) { 
    //assign an onKeyUp event
        return function() {
            var that = this,
                $this = $(this);
            clearTimeout($this.data('timeout'));
            $this.data('timeout', setTimeout(function(){
                //some code using variable 'field' and array 'ajaxFields'
                // "this" will not be your element in here, make sure to use "that" (or "$this")
            }, 1000));
        };
    }(fields[x]));
    

    I save the timeout in $this.data, so that each element can have its own timeout, instead of using a global variable.

    Updated Demo: http://jsfiddle.net/Z43Bq/3/

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

Sidebar

Related Questions

I have been trying to assign value to an object array that is defined
I'm trying to assign a class that I have within the jQuery UI accordion
I am trying to assign values within an array within the condition of a
I am trying to assign the onclick event to an anchor tag that already
I'm trying to assign the output of this command ( that is in my
I am trying to assign a regular expression result to an array inside of
I am trying to assign a form-inline class to the following form in rails
I am trying to assign an function within an function object property without actually
i have a menu item, and i'm trying to assign its OnClick event handler:
I am trying to assign true/ false using for loop for (i=1;i<31;i++){ _global.level +

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.