I have the following Onclick
event:
JavaScript
x
2
1
onClick="document.body.style.background = 'url(galleries/g2.jpg)'"
2
and when I attach it to:
JavaScript
1
2
1
<a onClick="document.body.style.background = 'url(galleries/g2.jpg)'"> blah</a>
2
all works fine, but I want it as a body onclick
event as follows:
JavaScript
1
2
1
<body class="g" onClick="document.body.style.background = 'url(galleries/g2.jpg)'">
2
and I get no dice.
The only thing to note would be that the class “g” already has a background image specified, but I don’t think that should matter.
Advertisement
Answer
Use this.
like this
JavaScript
1
2
1
<body onClick="this.style.background = 'url(http://placehold.it/20x50)';">
2
JSFiddle of the code above jsfiddle.net/mrRRG/2/. But please do not use this type of code construction. It’s better to make few CSS classes and use javascript to change them on event fire