Object doesn't support this property or method

hi,

I get an error when I try to dynamically add an attribute to some elements, since I been getting the error “Object doesn’t support this property or method” in IE I reduced the attribute value to just alert.


function initialize_containers()
{
	var containers = document.getElementsByClassName("boxcontainer");

	var i = 0;
	for (i = 0; i < containers.length; i++)
	{
		containers[i].setAttribute("onmouseover", "alert('hello world');");
	}
}

window.onload = function() { initialize_containers(); };

note that in firefox all the elements with the class boxcontainer gets “hello world” alert. I removed this specific code (shown above) out of the page and the error in IE goes away, so I’m 100% sure nothing else is causing it. Any suggestions?

No it only gets executed immediately if you add () to the end like this:

element.onmouseover = function() {iframeshim(parm1,parm2)}();

element.onmouseover = function() {iframeshim(parm1,parm2)};

Ah I didn’t know it can be used like that, always thought anything used within function() { … } got executed immediately.

That’s exactly the problem I’m facing now! I was just about to start a new thread, but was not sure whether I should or not.

Now that I can get elements with boxcontainer, I am now facing the problem with setAttribute(“onmouseover”, “alert(‘hello world’);”);

since there will be parameters can’t use it like element.onmouseover = iframeshim; where iframeshim function would have parameters.

Could anyone suggest alternative method to setAttribute?

Doesn’t IE also have issue with the setAttribute (esp with something like an attribute of “alert()”)??

You’ll need to emulate it: http://robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/

It uses span tag, but other than that assigning id’s to all the spam is not an option, is there a IE alternative workaround for getElementsByClassName?

AFAIK, IE’s document object doesn’t support getElementsByClassName()

Can you get them some other way? eg. Id or tag?