input checkbox onclick not working via DOM on IE7, FF, WebKit

A

Aaron Gray

The following input checkbox onclick via DOM is not working on IE7, FF, or
WebKit, but is working on Opera for some strange reason.

http://www.aarongray.org/Test/JavaScript/checkbox.html

Here's the code :-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
<script type="text/javascript" src="LibJS.js"></script>
</head>

<body onLoad="init()">

<script>

use( LibJS.HTML)

function init()
{
test = document.createElement( "INPUT")
test.type = "checkbox"
test.checked = true
test.onClick = "alert( 'test = ' + this.checked)"
// test.setAttribute( "onClick", "alert( 'test = ' + this.checked)")

document.body.appendChild( test)
}

</script>
</body>
</html>

Hope I have not missed something too silly.

Many thanks in advance,

Aaron
 
R

Richard Cornford

The following input checkbox onclick via DOM is not
working on IE7, FF, or WebKit, but is working on Opera
for some strange reason.
function init()
{
test = document.createElement( "INPUT")
test.type = "checkbox"
test.checked = true
test.onClick = "alert( 'test = ' + this.checked)"
Hope I have not missed something too silly.

In case sensitive javascript the property name to which you should be
assigning is 'onclick' (all lower case), and the value you should be
assigning is a reference to a function object not a string primitive.

Richard.
 
A

Aaron Gray

Richard Cornford said:
In case sensitive javascript the property name to which you should be
assigning is 'onclick' (all lower case), and the value you should be
assigning is a reference to a function object not a string primitive.

"function object not a string primitive" thats the one, silly, silly me !

Have not done some JavaScript for a couple of weeks, amazing how quickly my
brain chucks things out.

Aaron
 
S

SAM

Le 10/31/08 4:32 PM, Aaron Gray a écrit :
test.onclick = function() { alert('test = ' + this.checked); };
}
 
A

Aaron Gray

SAM said:
Le 10/31/08 4:32 PM, Aaron Gray a écrit :

test.onclick = function() { alert('test = ' + this.checked); };
}

Yes, thanks Sam, I now have everything working fine now :)

Aaron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top