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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:03:44+00:00 2026-06-10T13:03:44+00:00

Ok, had such problem … I’m trying to reduce amount of code, required to

  • 0

Ok, had such problem …
I’m trying to reduce amount of code, required to validate fields.
In this case I’ve created array with ids of elements which I wanted to bind events to.
Like this:

var fields = new Array('#edit-field-first-name', '#edit-field-last-name', '#edit-field-mobile');

Then I’m binding events in a loop:

for(var i = 0; i < fields.length; i++){
  if($('#user-profile-form ' + fields[i]).length > 0){
    var $obj = $('#user-profile-form ' + fields[i]);
    $obj.bind({
      blur :  function(){
                  if(!reg_chars.test($obj.val())){
                      $obj.css('border','1px solid #A14')
                          .parent().children('div.description').css('color', '#A14');
                  }else{
                      $obj.css('border-style','solid').css('border-color','#C5C3C3 #EDEDED #EDEDED #C5C3C3').css('border-width','1px')
                         .parent().children('div.description').css('color', '#333');
                  };
              }
    })
  }
}

And now strange things happen.
It’s triggering “blur” event properly. But it’s always referencing to last element in array. Looks like it’s always using the same object and just overwriting it every loop iteration.

So need help … What can be a solution?

  • 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-10T13:03:46+00:00Added an answer on June 10, 2026 at 1:03 pm

    The problem within context ($obj) assignment at bind function. Try to change to

        for(var i = 0; i < fields.length; i++){
          if($('#user-profile-form ' + fields[i]).length > 0){
            var $obj = $('#user-profile-form ' + fields[i]);
            $obj.bind({
              blur :  function(){
                          var $input = $(this);                      
    
                          if(!reg_chars.test($input.val())){
                              $input.css('border','1px solid #A14')
                                  .parent().children('div.description').css('color', '#A14');
                          }else{
                              $input.css('border-style','solid').css('border-color','#C5C3C3 #EDEDED #EDEDED #C5C3C3').css('border-width','1px')
                                 .parent().children('div.description').css('color', '#333');
                          };
                      }
            })
          }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If i had an image with a url such as img/160x180.jpg how using this
I had this doubt for sometime now, some people say that there's no such
Had this working; at one stage. The problem is the following text is now
I never had such a problem and I'm pretty puzzled: function delete_post(id) { var
I currently had a problem similar to this previous question: Why would our Java
This is a problem I've had to deal with in my last project, and
Maybe somebody had faced with such problem? I just have tried to use Video-JS
I'd like to ask if someone had such a problem before. I have version
I would like to get all possible available currencies. Java 7 had provided such
Had a problem with the recursive conflictCheck() method. That seems fine now. I have

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.