javascript - Help me understand how to attach function to events -
// param {name:'name', click : function(ev[,arg1, arg2] ){}, focus:function(ev[,arg1, arg2] ){} } togglebutton = function(params) { var btn = document.getelementid(params.name); // how hook above click function passed using // params , pass additional params along function(ev[,arg1,arg2]) btn.onclick = params.click; }
i trying implement extended link button act toggle button. able attach event specified above not getting how attach buttons can enjoy code below?
new togglebutton({name: 'toggle1', "click" : function(evt, obj1, obj1){ //function body } });
i way:
<a href="foo.html" id="toggle1">toogle</a> var togglebutton = function(params) { var btn = document.getelementbyid(params.id); var evt = 'on' + params.evt; btn[evt] = params.action; return; } togglebutton({ id: 'toggle1', evt: 'click', action: function(evt){ var clickedelement = this; alert('my href '+ clickedelement.getattribute('href')); // prevent browser following href, // in case set attribute var e = evt || window.event; e.preventdefault(); } });
Comments
Post a Comment