[Javascript] item.onclick = "alert('foo');";

Roger Roelofs rer at datacompusa.com
Mon Nov 7 05:43:58 CST 2005


Anthony,

On Nov 6, 2005, at 6:01 PM, Anthony Ettinger wrote:

> this doesn't seem to work:
>
> var item = document.getElementById('foo');
> item.onclick = "alert('foo');";
>
> http://developer.mozilla.org/en/docs/DOM:element.onclick

I wouldn't expect it to.  You need to assign a function to onclick 
rather than some text.

var item = document.getElementById("foo");
if ( item ) {
	item.onclick = function() { alert("foo") };
}

I've never seen it done the way it is in the first example on the page 
you cite.  I could be wrong, but it looks like a documentation bug to 
me.

hth
Roger,

Roger Roelofs
Know what you value.




More information about the Javascript mailing list