Passing parameter to invoked event handler, i.e. element.onchange(); javascript -



Passing parameter to invoked event handler, i.e. element.onchange(); javascript -

i have function this:

function dosomething() { // select element } document.getelementbyid("selectel").onchange = dosomething; // phone call onchange event document.getelementbyid("selectel").onchange();

now, recognize phone call function straight , pass parameter. i'd know if it's possible pass parameter onchange() event handler after it's evoked. tried

document.getelementbyid("selectel").onchange("hello");

, didn't work.

thank help.

you need bind parameter function. i'm going re-create paste function ext-js lets that. warning: not beginners

/** * create new function provided <code>fn</code>, alter <code>this</code> provided scope, optionally * overrides arguments call. (defaults arguments passed caller) * * @param {function} fn function delegate. * @param {object} scope (optional) scope (<code><b>this</b></code> reference) in function executed. * <b>if omitted, defaults browser window.</b> * @param {array} args (optional) overrides arguments call. (defaults arguments passed caller) * @param {boolean/number} appendargs (optional) if true args appended phone call args instead of overriding, * if number args inserted @ specified position * @return {function} new function */ function bind(fn, scope, args, appendargs) { var method = fn, applyargs; homecoming function() { var callargs = args || arguments; if (appendargs === true) { callargs = array.prototype.slice.call(arguments, 0); callargs = callargs.concat(args); } else if (typeof appendargs == 'number') { callargs = array.prototype.slice.call(arguments, 0); // re-create arguments first applyargs = [appendargs, 0].concat(args); // create method phone call params array.prototype.splice.apply(callargs, applyargs); // splice them in } homecoming method.apply(scope || window, callargs); }; }

you can utilize like

function onchange(e, customparameter) { // whatever code } document.getelementbyid("selectel").onchange = bind(onchange, null, ["customparameter"], true);

when handler called, additional parameters appended arguments passed event handler (the event object).

there's lot of meat in function, sense free inquire additional questions.

here's jsfiddle see in action http://jsfiddle.net/ybhg6/

javascript events parameters handler

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -