Skip to content
Advertisement

Body OnClick doesn’t work… but the onclick works when attached to a tag?

I have the following Onclick event:

onClick="document.body.style.background = 'url(galleries/g2.jpg)'"

and when I attach it to:

<a onClick="document.body.style.background = 'url(galleries/g2.jpg)'"> blah</a>

all works fine, but I want it as a body onclick event as follows:

<body class="g" onClick="document.body.style.background = 'url(galleries/g2.jpg)'">

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

<body onClick="this.style.background = 'url(http://placehold.it/20x50)';">

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

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement