Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-29-2008, 06:34 PM   PM User | #1
scott212
New to the CF scene

 
Join Date: Apr 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
scott212 is an unknown quantity at this point
[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));
},
scott212 is offline   Reply With Quote
Old 12-29-2008, 07:13 PM   PM User | #2
scott212
New to the CF scene

 
Join Date: Apr 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
scott212 is an unknown quantity at this point
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
scott212 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:54 PM.


Advertisement
Log in to turn off these ads.