...

View Full Version : dynamic set onclick attribute for IE



mglowacki
05-09-2006, 07:14 AM
Hi

I know it's a common problem, but found solutions didn't help me at all. So, I have a code fragment, which works fine for firefox but doesn't work at all in IE. I'm trying to set onclick attribute on span element (make a 'dynamic' link for ajax usage):

span = document.createElement("span");
span.id = "gameRowRemove" + id;
span.setAttribute("className","link");
span.setAttribute("onclick",removeGame(this));
span.appendChild(document.createTextNode("remove"));

addEventListener gives 'method not supported by this object' error.

Thanks in advance,
Michal

PS. Is there any tool to debug scripts in IE? something like javascript console in firefox? Errors reported by IE are not cleared until browser is restarted, confusing line numbers, sometimes no error at all and script doesn't work...

jkd
05-09-2006, 07:37 AM
span.setAttribute("onclick","removeGame(this)");

mglowacki
05-09-2006, 07:43 AM
already tried that, gives no error, but nothing happens after click (in IE of course)

Kor
05-09-2006, 08:08 AM
use an anonymous. And use DOM 0 syntax in case of class.

span.className="link";
span.onclick=function(){removeGame(this)}

Bill Posters
05-09-2006, 08:22 AM
use an anonymous. And use DOM 0 syntax in case of class.
...and events. It's a known bug that events added using setAttribute in IE will register in the attr dom, but won't actually work.

(I know Kor used DOM 0 in the example, but I just thought it needed a specific mention.)

charlie.
05-09-2006, 08:24 AM
Kor's code is best, but missing semi-colons. Use...


span.onclick= function() { removeGame(this); };

Both semi-colons are necessary!

_Aerospace_Eng_
05-09-2006, 08:30 AM
Kor's code is best, but missing semi-colons. Use...

Both semi-colons are necessary!
No they aren't. It should still work without them.

Kor
05-09-2006, 09:39 AM
Kor's code is best, but missing semi-colons. Use...


span.onclick= function() { removeGame(this); };

Both semi-colons are necessary!
None of them are required. Semicolons mark the end of the statement. The same do the } bracket, so that both ;} and }; are redundant

mglowacki
05-09-2006, 02:16 PM
that solved the problem, thanks guys!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum