PDA

View Full Version : How to get element from inside event handler



cwells
20 Aug 2010, 1:11 PM
I want to add a class to an element that has been clicked on:



Ext.select ("div#topmenu ul li a").on ("click", (
function (e, t, o) {
e.stopEvent();
Ext.get ("middle").load ({
url: this,
scripts: true
});
Ext.select ("a.active").removeClass ("active");
t.addClass ("active"); // ERROR
}
));


Everything works up to the last line, where t.addClass() gives me the following error:



Uncaught TypeError: Object #<an HTMLElement> has no method 'addClass'


I am unable to locate any documentation on "HTMLElement", which I assume is distinct from Ext.Element.

In any case, how can I get add a class to this element?

Condor
20 Aug 2010, 10:36 PM
It's:

Ext.fly(t).addClass('active');

cwells
21 Aug 2010, 2:39 AM
Hrm, still doesn't work (although no errors now). Here's the complete code:



Ext.onReady ( function () {
Ext.BLANK_IMAGE_URL = "/js/extjs/resources/images/default/s.gif";

Ext.select ("div#topmenu ul li a").on ("click", (
function (e, t, o) {
e.stopEvent();
Ext.get ("middle").load ({
url: this,
scripts: true
});
Ext.select ("a.active").removeClass ("active");
Ext.fly (t).addClass ("active");
}
));
});


This isn't a huge deal for me as the jQuery code is working fine (I was just hoping to reduce my dependencies a bit), but if it *should* work, then consider this a bug report ;-)

Condor
21 Aug 2010, 3:12 AM
I would use:

Ext.onReady(function () {
Ext.get('topmenu').on('click', function(e){
Ext.get('middle').load({
url: e.getTarget().href,
scripts: true
});
Ext.select('#topmenu a.active').removeClass('active');
Ext.fly(e.getTarget()).addClass('active');
}, null, {delegate: 'a', stopEvent: true});
});