SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2002
    0 Post(s)
    0 Thread(s)

    Binding object method to event

    I'm trying to build a Validation object, where you pass an object id and event name in the constructor. Then, you call an add_rule method to add validation rules. The constructor code looks something like:

    function Validation(id, event, ...)
    this.object = document.all[id];
    this.event = event;
    var obj = this.object;
    obj[event] = this.validate;

    So the actual validation function is dynamically bound to the appropriate event. The problem is that when the validate method is called, "this" no longer refers to the Validation object; it refers to the object to whose event it was bound. So, for instance, in the following code, the validate function's "this" refers to frmSubmit, not objValidate:

    objValidate = new Validation ("frmSubmit", "onsubmit", ...);

    Of course, I need to refer to the actual validation object's properties, but I can't figure out how. Any suggestions?

  2. #2
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Gatwick, UK
    0 Post(s)
    0 Thread(s)
    First off: congratulations - this is what i like to see - a real use of functions and object passing

    Now, some info:

    You could return the object in order to get it it created for sure ( though you needn't really, and it's archaic cobol throw back that i definate frown upon ).

    surely though, you are pretty much there (except for trying to go backwards up a stream)

    <input name="cat" id="cat">
    <script language="Javascript">
    function Validate(id, event, action){
            this.object = document.all[id];
    function act(){
    var inp = new Validate('cat','onclick','act');


Posting Permissions

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