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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:21:37+00:00 2026-05-26T19:21:37+00:00

i have an array of objects, and these objects all have an ‘isvalid’ attribute.

  • 0

i have an array of objects, and these objects all have an ‘isvalid’ attribute.

Is there a way with JQuery or plain javascript to bind code to the event that the value of that property changes?

So when i have an array with 10 objects, i want to execute a function when the ‘isvalid’ property of one of the object changes.

Is that possible?

Michel

  • 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-26T19:21:37+00:00Added an answer on May 26, 2026 at 7:21 pm

    It’s possible with plain JavaScript by using a property setter function. Using ES5 syntax, that looks like this (live example — works in Chrome and other browsers with reasonable ES5-compliance):

    // Our constructor function
    function Thing() {
    }
    
    // Define a "foo" property with a setter and getter
    Object.defineProperty(Thing.prototype, "foo", {
      set: function(value) {
        this.fooStorage = value;
        display("Foo was set to '" + value + "'");
      },
      get: function() {
        return this.fooStorage;
      }
    });
    
    // Create a `Thing`  
    var t = new Thing();
    
    // Set its property
    t.foo = "bar";
    

    When the t.foo = "bar"; assignment is executed, the setter function is called. You can have the setter function call a callback if you like, to notify you that the Thing object was changed.

    Note that the above is just an example. It uses the fooStorage property to store the value of foo, which is less than ideal but nice and simple for an example.

    To do this in a way that’s compatible with non-ES5 JavaScript engines, you either have to fall back on some proprietary and now-deprecated syntax from Mozilla (which won’t work on other engines), or just use an explicit setter function (live example):

    // Our constructor function
    function Thing() {
    }
    
    // Define a "foo" property with a setter and getter
    Thing.prototype.setFoo = function(value) {
      this.fooStorage = value;
      display("Foo was set to '" + value + "'");
    };
    Thing.prototype.getFoo = function() {
      return this.fooStorage;
    };
    
    // Create a `Thing`
    var t = new Thing();
    
    // Set the property
    t.setFoo("bar");
    

    (And again, this is just an example using a simplistic means of storing foo‘s value.)

    This has the advantage that it works with just about any JavaScript engine, not just ES5-compliant ones, and it’s explicit that setting foo is a function call, not just a property assignment (whereas with the ES5 setter/getter syntax, the person setting the foo property doesn’t know that it’s a function call — which has upsides and downsides).

    So that’s how you capture the fact that the property changed. Then it’s just a matter of allowing callbacks to be registered and removed to receive notification of changes. These are easily managed in a simple array. Here’s an ES5-based example doing it on a per-object basis; obviously you could do this in some kind of grouped way instead for the entire array of objects you want to let people watch. (live copy)

    window.onload = function() {
    
      // Our constructor function
      function Thing() {
        this.fooHandlers = [];
      }
    
      // Add a listener for "foo" changes
      Thing.prototype.addFooChangeHandler = function(callback) {
        this.fooHandlers.push(callback);
      };
    
      // Remove a listener for "foo" changes
      Thing.prototype.removeFooChangeHandler = function(callback) {
        var index;
    
        index = this.fooHandlers.indexOf(callback);
        if (index >= 0) {
          this.fooHandlers.splice(index, 1);
        }
      };
    
      // Define a "foo" property with a setter and getter
      Object.defineProperty(Thing.prototype, "foo", {
        set: function(value) {
          var index;
    
          for (index = 0; index < this.fooHandlers.length; ++index) {
            try {
              // Handler receives a reference to this Thing,
              // foo's old value, and foo's new value.
              this.fooHandlers[index](this, value, this.fooStorage);
            }
            catch (e) {
            }
          }
    
          this.fooStorage = value;
        },
        get: function() {
          return this.fooStorage;
        }
      });
    
      // Create a `Thing`
      var t = new Thing();
    
      // Add a foo change handler
      t.addFooChangeHandler(function(t, newValue, oldValue) {
        display("Handler 1: Foo changed from '" + oldValue + "' to '" + newValue + "'");
      });
    
      // Add another
      t.addFooChangeHandler(function(t, newValue, oldValue) {
        display("Handler 2: Foo changed from '" + oldValue + "' to '" + newValue + "'");
      });
    
      // Set the property
      t.foo = "bar";
      t.foo = "boo";
    
      // === Basic utility functions
    
      function display(msg) {
        var p = document.createElement('p');
        p.innerHTML = msg;
        document.body.appendChild(p);
      }
    };
    

    To do that without an ES5 JavaScript engine, just set the setFoo / getFoo model described earlier, and make sure the engine supports Array#indexOf correctly (some engines don’t have it at all, some use == rather than === equivalence) or replace the use of Array#indexOf in removeFooChangeHandler with a simple loop through the array looking for the callback:

    // Remove a listener for "foo" changes
    Thing.prototype.removeFooChangeHandler = function(callback) {
      var index;
    
      for (index = 0; index < this.fooHandlers.length; ++index) {
        if (this.fooHandlers[index] === callback) {
          this.fooHandlers.splice(index, 1);
          break;
        }
      }
    };
    

    Side note: There are a number of anonymous functions in these examples. I’ve done that to avoid making things seem complex, but I’m not a fan of anonymous functions, I prefer that functions have names. See the link for details.

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

Sidebar

Related Questions

I have a JSON array with ActiveRecord objects. These objects can be reconstructed using
I have an array of pointers to Objective-C objects. These objects have a sort
I have an Array of NSDictionary objects. These Dictionaries are parsed from a JSON
I have an array of objects. If I call unset($array), will it unset all
I have a multidimensional array within these arrays contains array objects. How can I
I have an application that has an array of 5 objects. These objects are
So, I have an array that contains X objects, all named by dates and
Is there any method like the array_unique for objects? I have a bunch of
I have an array of objects which populate a UITableView . When a user
I have an Array of Objects that need the duplicates removed/filtered. I was going

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.