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

  • Home
  • SEARCH
  • 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 7855421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:22:57+00:00 2026-06-02T20:22:57+00:00

Im curious about what might be a larger question than I think. I am

  • 0

Im curious about what might be a larger question than I think.

I am using the following code to listen for ‘keyup’ on a group of text input fields. If the user stops typing for a given amount of time, I send the data to a controller using AJAX.

I decided to try my hand at OOP in javascript to accomplish this. This is because I want a new instance of the timer method for each input field. (To be absolutely clear, Im very new to OOP in javascript so this might be dreadful. Let me know.)

Here is the main class with its methods:

    function FieldListener(entity){
      t = this;

      t.typingTimer;                // Timer identifier
      t.doneTypingInterval = 1000;  // Time in ms. e.g.; 5000 = 5secs        
      t.entity = entity;

      entity.bind("keyup", function(){t.setTimer();});
    }

    FieldListener.prototype.setTimer = function(){
      t = this;

      // User is still typing, so clear the timer.
      clearTimeout(t.typingTimer);

      // Get the field name, e.g.; 'username'
      t.entityType = t.entity.attr("name");
      // If the value is empty, set it to a single space.
      if(!(t.val = t.entity.val())){
         t.val = ' ';
      }
      t.noticeSpan = t.entity.siblings("span");
      // Display 'waiting' notice to user.
      t.noticeSpan.html('...')

      t.typingTimer = setTimeout(function(){t.doneTyping();},t.doneTypingInterval);
    }

    FieldListener.prototype.doneTyping = function(){
       // Encode for passing to ajax route.
      t = this;

      valueType = encodeURIComponent(t.entityType);
      value = encodeURIComponent(t.val);

      $.ajax({
        url: '/check/'+valueType+'/'+value,
        type: 'GET',
        processData: false
      })
      .done(function(validationMessage){
        t.noticeSpan.html(validationMessage);
      })
      .fail(function(){
        t.noticeSpan.html("Something went wrong. Please try again.");
      });       
    }

So from here I’d like to be able to create an object of the FieldListener class for every input field.

I know I can do it easily if I have an id for each like so:

   var fieldListener = new FieldListener($("#someFieldID"));

But I’d like to iterate over every field with a given class name. Something close to this perhaps?:

   i = 0;
   $(".info-field").each(function(){
      i = new FieldListener($(this));
   });

But that doesn’t work (and doesn’t look very nice).

Any thoughts? (Im also curious about critiques/improvements to the class/methods code as well.)

edit: As per @ChrisHerring’s question: The issue is that it seems to create the object but only for the last element in the each() method. So the span associated with the last input field with the class ‘.info-field’ displays the validationMessage returned from AJAX regardless of which field I am typing in.

UPDATE:

It seems like something is wrong with the creation of new objects. For example, if, rather than iterating through the each() method, I simply follow one class initiation with another, like so:

   var fieldListener1 = new FieldListener($("#someFieldID"));
   var fieldListener2 = new FieldListener($("#someOtherFieldID"));

that fieldListener2 overwrites variables being saved when initiating fieldListener1. This means that when I type into the input field with id “#someFieldID”, it behaves as if I am typing into the input field with id “#someOtherFieldID”. Thoughts?

UPDATE #2 (solved for now):

It seems that I have solved the issue for now. I needed to add ‘var’ before ‘t = this;’ in the FieldListener class. Any comments/critiques are still welcome of course. 😉

  • 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-02T20:23:00+00:00Added an answer on June 2, 2026 at 8:23 pm

    The t variable is global. The function for the “keyup” event is evaluated dynamically which means it picks up the last value of t.

    Change

      t = this;
    

    to

      var t = this;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question might get closed, but I'm curious about whether it's always required to
I'm curious about the following code: class MyClass { public: MyClass() : _myArray(new int[1024])
It might be a silly question but I am just curious about what goes
Curious about how developers more experienced with Django than I might handle this: I
You might see this as a dumb question, but I'm curious about how I
This might sound a little stupid but I am still curious about what the
my question for today is that i'm curious about if there is a close
I've been curious about this for a while now, when using structures inside of
I am curious as to how I would go about reading the input from
Curious about running multiple xvfb displays: I have between 10-50 instances of a script

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.