html - Javascript function argument setting through DOM -
i have select element i'm creating through javascript (dom)
var cell_stock_id = row.insertcell(0); var cell_stock_id_sel = document.createelement('select');
i have kept onchange javascript function this-
cell_stock_id_sel.setattribute("onchange","populateotherfields");
the function gets called when change selected option in select.
is there way can pass argument function "populateotherfields" ?
i have select box , lot of text fields in table row. want populate rows based on selection. i'm able access data has inserted in fields, don't know how enter data in textfields row. i'm thinking of passing unique number identify names of elements in row, , javascript parse pattern in names of elements of row.
document.forms["0"].elements[i].name.indexof("pattern").
so there way can pass "pattern", iteration (no. of rows, row number) number.
select__1, text__1, text2__1 select__2, text__2, text2__2
the '__1' , , '__2' need passed. , they're not static patterns. number.
don't use setattribute
set event handlers, use direct assignment or (better) addeventlistener
/ attachevent
, can create function call function desired arguments. (you anyway, of course, using setattribute
you'd either have inline or create yet global, named function.)
direct assignment:
cell_stock_id_sel.onchange = function() { populateotherfields(arguments, here); };
works on browsers, can have single change
handler on element using method (as setattribute
).
addeventlistener
:
cell_stock_id_sel.addeventlistener('change', function() { populateotherfields(arguments, here); }, false);
this the standard, doesn't exist on older versions of ie.
attachevent
:
cell_stock_id_sel.attachevent('onchange', function() { populateotherfields(arguments, here); });
this ie-only (er, mostly, browsers throw bone ie-only sites). it's microsoft's pre-standard way of doing it. in ie8 , above in standards mode, can use addeventlistener
instead.
another advantage of all of these methods don't have have global functions, whereas setattribute
way, if use function name, function has global. global namespace on browsers really crowded, recommend avoiding adding clutter.
because of various vagaries between browsers, tend recommend using decent javascript browser library such jquery, yui, closure, or any of several others. these smooth on differences addeventlistener
/ attachevent
thing , provide lot of helpful utility functionality.
Comments
Post a Comment