Featured post
javascript - Problem with Event Handling via YUI -
when users click "search" input element, search text inside input disappear , since have several controls that, thought make code reusable. here code formerly done , working jquery in yui cannot make work.
var subscriptionboxtarget = "div.main div.main-content div.side-right div.subscription-box input"; var ssbnode = yahoo.util.selector.query(subscriptionboxtarget); var ssbvalue = yahoo.util.dom.getattribute(ssbnode,"value"); var subscriptionbox = new removabletext(ssbnode,ssbvalue,null); subscriptionbox.bind(); //////////////////////////////// //target : target of element dispatches event // defaulttext : default input[type=text] elements // callback : function run after everthing completed function removabletext(target,defaulttext,callback) { var target = target; //private members var defaulttext = defaulttext; var callback = callback; //instance method this.bind = function() { mouseclick(target,defaulttext); mouseoff(target,defaulttext); if(callback != null) callback(); } //private methods var mouseclick = function(eventtarget,defaultvalue) { var _eventtarget = eventtarget; var _defaultvalue = defaultvalue; /*$(eventtarget).bind("click",function(){ var currentvalue = $(this).val(); if(currentvalue == defaultvalue) $(this).val(""); });*/ yahoo.util.event.addlistener(_eventtarget,"click",function(e){ alert(e); }); } var mouseoff = function(eventtarget,defaultvalue) { var _eventtarget = eventtarget; var _defaultvalue = defaultvalue; /*$(eventtarget).bind("blur",function(){ var currentvalue = $(this).val(); if(currentvalue == "") $(this).val(_defaultvalue); });*/ yahoo.util.event.addlistener(_eventtarget,"blur",function(e){ alert(e); }); } }
you have lot of unnecessary code here.
the input parameters passed removabletext constructor available closure methods defined inside. don't need to, , shouldn't redefine named params vars.
function removabletext(target, defaulttext, callback) { this.bind = function () { yahoo.util.event.on(target, 'click', function (e) { /* can reference target, defaulttext, , callback in here */ }); yahoo.util.event.on(target, 'blur', function (e) { /* , here */ }); if (callback) { callback(); } }; }
the definition of instance method within constructor seems dubious, requirement values passed constructor must kept private. assign them instance properties (this._target = target; etc) , add instance methods prototype. if functionality you're after simple, why bother methods @ all?
using click event not support keyboard navigation. should use focus event.
i'm not sure why have callback passed @ construction fires after attaching event subscribers.
- Get link
- X
- Other Apps
Comments
Post a Comment