PDA

View Full Version : how to add a Listener for each node in a array of nodes!



enotsl
13 Sep 2008, 11:05 PM
<ul id="box">
<li>aa</li>
<li>bb</li>
<li>cc</li>
</ul>


I want to add a listener to each 'LI' tag use the below code



Ext.get('box').query('li').on('click',function(){alert('1111')},this)


but it's not work, I have search the API document, but still not find the answer,

Please help! thanks!

santosh.rajan
13 Sep 2008, 11:29 PM
Ext Elements and javascript dom elements are not the same things. After doing Ext.get you need to use the javascript dom functions to achieve what you want. Read the javascript dom documentation instead.

enotsl
13 Sep 2008, 11:41 PM
thank you very much!

I use the Ex.get(el), it's work!



var ul=Ext.get('box');
Ext.get(ul.query('li')).on('click',function(){
alert(this.dom.innerHTML)
},this)
But another question is coming!

When i click the 'LI" Tag, I want to get the inner HTML in the 'LI' tag which is be clicked, but is not work.

The firebug's error message is "this.dom is undefined", I think the error is the scope option no correct, but I don't know how the set the scope option, i try the use the " Ext.get(ul.query('li'))" to replace "this", but it's still no work.

evant
13 Sep 2008, 11:47 PM
Look at the return types.

Query returns an array, select returns a CompositeElement:

http://extjs.com/deploy/dev/docs/?class=Ext.Element&member=select

evant
13 Sep 2008, 11:48 PM
Ext.get('foo').select('li').on('click', function(e)
{
var el = e.getTarget(null, null, true);
alert(el.dom.innerHTML);
}
);

enotsl
14 Sep 2008, 12:39 AM
thanks, it works fine!

Animal
14 Sep 2008, 7:53 AM
Or



Ext.get('box').on('click',function(e){
alert(e.getTarget().innerHTML);
},this, {delegate: 'li'});