What a pain. I just pressed "Preview Post" only to find that the server forgot who I was and killed my input. So, where once I had everything described really nice, now I'm just going to write my code and assume everyone is intelligent enough to figure it out:
[html]
<html>
<body onLoad="init(); ">
<div id="b"></div>
</body>
</html>
[/html]
[code=javascript]
function init() {
var a = test(10,'b');
a.init();
alert("I just initialized " + a.buttons + " buttons inside " + a.container.id + "!");
}
var test = function( _buttons, _container ) {
var that=this;
// public properties
that.buttons = _buttons;
that.container = document.getEle mentById(_conta iner);
// public methods
that.init = function() {
for(x=0;x<parse Int(that.button s);x++) {
var b = document.create Element("INPUT" );
b.type="button" ; b.id = x; b.value = x;
/* how best to attach an event? */
// b.onclick = function() { that.tester(x) }
// when buttons is clicked, always alerts 10.
// eval("b.onclick = function() { that.tester('"+ x+"') }")
// when clicked, alerts correctly, "0-9", but what an ugly hack.
that.container. appendChild( b );
}
}
that.tester = function( _e ) { alert(_e); }
return that;
}
[/code]
Anyone know how I can avoid using eval on the event attachment? It will work for strings -- but if I get into objects, maybe as part of a "for x in b" on some data structure I need to turn into dom elements -- I don't think I can pass those in single quotes. If I pass a variable reference, the same reference is used for each attachment in the loop, and it will end up being the last property in the loop for each attachment.... Any suggestions?
p.s. I don't trust the "this" keyword.
[html]
<html>
<body onLoad="init(); ">
<div id="b"></div>
</body>
</html>
[/html]
[code=javascript]
function init() {
var a = test(10,'b');
a.init();
alert("I just initialized " + a.buttons + " buttons inside " + a.container.id + "!");
}
var test = function( _buttons, _container ) {
var that=this;
// public properties
that.buttons = _buttons;
that.container = document.getEle mentById(_conta iner);
// public methods
that.init = function() {
for(x=0;x<parse Int(that.button s);x++) {
var b = document.create Element("INPUT" );
b.type="button" ; b.id = x; b.value = x;
/* how best to attach an event? */
// b.onclick = function() { that.tester(x) }
// when buttons is clicked, always alerts 10.
// eval("b.onclick = function() { that.tester('"+ x+"') }")
// when clicked, alerts correctly, "0-9", but what an ugly hack.
that.container. appendChild( b );
}
}
that.tester = function( _e ) { alert(_e); }
return that;
}
[/code]
Anyone know how I can avoid using eval on the event attachment? It will work for strings -- but if I get into objects, maybe as part of a "for x in b" on some data structure I need to turn into dom elements -- I don't think I can pass those in single quotes. If I pass a variable reference, the same reference is used for each attachment in the loop, and it will end up being the last property in the loop for each attachment.... Any suggestions?
p.s. I don't trust the "this" keyword.
Comment