3

In a simple html page I have:

<SCRIPT>
function Clicker(number){
if (number == 1) 
document.write ('<style type="text/css">body {background-color: #cccccc;}</style>');
}
</SCRIPT>

and in the html body:

<a onmouseclick="Clicker(1)" href="#">clic</a>

But when I click on the link nothing happens. Where am I wrong?

tic
  • 4,009
  • 15
  • 45
  • 86
  • Why are you doing it this way? Why don't you just set the background of body with javascript, like style.backgroundColor= or apply a class that has a black background? – Jarrett Widman Mar 14 '10 at 00:20
  • afaik it's onclick, not onmouseclick – stex Mar 14 '10 at 00:20
  • 1
    If you do a `document.write` this way (after the document has completely loaded), it will replace the entire page contents with the thing you just wrote(the style element in your case) – Chetan S Mar 14 '10 at 00:27

3 Answers3

2

Writing the style block probably won't effect a change in the background colour. To do that you have to manipulate the Document object:

<script type="text/javascript">
function Clicker(number){
if (number == 1) 
    document.body.style.background='#cccccc';
}
</script>

Your click event should also be onclick:

<a onclick="Clicker(1)" href="#">clic</a>
Stephen Perelson
  • 6,613
  • 3
  • 21
  • 15
1

Simply change onmouseclick to onclick.

Eoinii
  • 2,457
  • 1
  • 17
  • 12
0

You need 'onclick' like following.

<a onclick="Clicker(1)" href="#">clic</a>
SoftwareGeek
  • 15,234
  • 19
  • 61
  • 78