SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Observe change of boolean variable

    Is it possible to observe a boolean variable in javascript?

    like Event.observer(element,"change",function(){..})

    in DOM2 there are only eventnames for form elements "change" or mouseevents.

    thanks
    manni

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    How would this "boolean variable" change? What defines change?

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use something like the the Yahoo UI Library to create your own custom events. Here's an example:
    Code HTML4Strict:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
     "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset:utf-8">
      <title></title>
    </head>
    <body>
    <script type="text/javascript" src="http://yui.yahooapis.com/2.4.1/build/yahoo-dom-event/yahoo-dom-event.js"></script>
    <script type="text/javascript">
    function TestObj() {
        // Creating a private variable that can only be set using setMyBoolean
        var myBoolean = true;
        // define a custom event
        this.myBooleanChange = new YAHOO.util.CustomEvent("myBooleanChange", this);
        this.setMyBoolean = function(b) {
            myBoolean = b;
            this.myBooleanChange.fire(b);
        }
        this.getMyBoolean = function() {
            return myBoolean;
        }
    }
    // create an instance of our test object
    var t1 = new TestObj();
    // Subscribe to the event
    t1.myBooleanChange.subscribe(function(type, args) {
        alert('Anonymous function received event: ' + type + '\nValue: ' + args[0]);
    });
    // fire the test object's event
    t1.setMyBoolean(true);
    </script>
    </body>
    </html>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •