PDA

View Full Version : appending an event listener



hicker
20 Feb 2007, 12:24 PM
hi, i want to create an input and handle some events of that input but couldn't make it. could somebody help about the code below. i get no error messages but it doesn't work.

var handleFocus = function(e){
alert('ok');
};
var parentElement = getEl('div1');
var element = YAHOO.ext.DomHelper.append(parentElement, {tag:'input', type:'text', size:'15'}, true);
element.mon('focus',handleFocus,this,true);

BernardChhun
23 Feb 2007, 7:47 PM
hi, i want to create an input and handle some events of that input but couldn't make it. could somebody help about the code below. i get no error messages but it doesn't work.

var handleFocus = function(e){
alert('ok');
};
var parentElement = getEl('div1');
var element = YAHOO.ext.DomHelper.append(parentElement, {tag:'input', type:'text', size:'15'}, true);
element.mon('focus',handleFocus,this,true);

I must say I've never used the .mon() function before...but this works for me perfectly:


element.addListener('focus', function(){alert('ok')});

brian.moeskau
23 Feb 2007, 10:41 PM
hicker,

The problem is that DomHelper.append expects a DOM node, not an Ext.Element, as the parent parameter. Change the param to parentElement.dom:


var handleFocus = function(e){
alert('ok');
};
var parentElement = getEl('div1');
var element = YAHOO.ext.DomHelper.append(parentElement.dom, {tag:'input', type:'text', size:'15'}, true);
element.mon('focus',handleFocus,this,true);
Alternatively (and more simply) you can simply pass in the parent ID and DomHelper will take care of the element lookup for you internally:


var handleFocus = function(e){
alert('ok');
};
var element = YAHOO.ext.DomHelper.append('div1', {tag:'input', type:'text', size:'15'}, true);
element.mon('focus',handleFocus,this,true);

And you may already know if you've been following the Alpha discussions, getEl() and Element.mon() are being deprecated in 1.0 (in favor of Ext.get() and Element.on() -- which will return an Ext event object by default -- respectively).

The changes to the event API in particular are worth noting more closely:
http://www.yui-ext.com/forum/viewtopic.php?t=2520

HTH,
Brian