I would like to create the following
[CODE]
fnTest(sId,sTxt) {
alert(sId + ‘: ‘ + sTxt);
}
…
<a href=”#” id=”i23″ onClick=”fnTest(this.id,’Iiyama ProLite E511S’)”>E511S</a>
If you write it all out in HTML this works fine. I need to create this dynamically, which works fine up to the definition of de onClick event handler:
[CODE]
var eA = doc.createElement(“a”);
eA.setAttribute(“id”,”d23″);
eA.setAttribute(“href”,”#”);
eA.setAttribute(“onClick”,”fnTest(this.id,’Iiyama ProLite E511S’)”);
FF executes the code, IE does nothing (but also no error). Another approach would be to attach an eventHandler to the element. I used Nicholas C. Zakas’ code and changed the function:
[CODE]
fnTest() {
alert(‘Testing 123′);
}
var EventUtil = new Object;
EventUtil.addEventHandler = function (oTarget, sEventType, fnHandler) {
if (oTarget.addEventListener) { // DOM
oTarget.addEventListener(sEventType, fnHandler, false);
}
else if (oTarget.attachEvent) { //IE
oTarget.attachEvent(“on”+sEventType, fnHandler);
}
else { // ? what else is there ?
oTarget[“on”+sEventType] = fnHandler;
}
};
var eA = doc.createElement(“a”);
eA.setAttribute(“id”,”d23″);
eA.setAttribute(“href”,”#”);
EventUtil.addEventHandler(eA,’click’,fnTest);
Executes in both browsers, but now I am unable to pass parameters. So I changed the function to:
[CODE]
fnTest() {
alert(this.id);
}
FF executes OK, IE7 comes with a box that says: “undefined”.
I see at least one last alternative: innerHTML. I have never worked with it and since it is not a DOM standard, I’d rather not use it. If it can make things work I’ll use it, but only if nothing else can.
Anyone any ideas?
Thanks,Erik.