Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    [Prototype] Adding arguments to event handlers

    In the code below, why does 'index' come back as undefined when the row is clicked on? Is it being evaluated when the event is called and thus is through it's lifecycle? How should I do this?
    Code:
    _populateTable: function(data) {
    	// remove children of 'this.tbody' if exists so that data can be reloaded
    	
    	data.each(function(item,index) {
    		var tr = new Element('tr',{'id':index});
    		this._tbody.appendChild(tr);
    		
    		tr.observe('click', function(event,index){
    			alert(index);
    		});
    		
    		this._columnModel.each(function(column) {
    			var td = new Element('td').update(item[column]);
    			tr.appendChild(td);
    		}.bind(this));
    	}.bind(this));
    },

  • #2
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I love prototype. Inside your handler function, 'this' is the element that initiated the event.

    like this:
    Code:
    _populateTable: function(data) {
    	// remove children of 'this.tbody' if exists so that data can be reloaded
    	
    	data.each(function(item,index) {
    		var tr = new Element('tr',{'id':index});
    		this._tbody.appendChild(tr);
    		
    		tr.observe('click', function(event){
    			alert(this.id);
    		});
    		
    		this._columnModel.each(function(column) {
    			var td = new Element('td').update(item[column]);
    			tr.appendChild(td);
    		}.bind(this));
    	}.bind(this));
    },
    Reference: http://prototypejs.org/api/event/observe


  •  

    Posting Permissions

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