change class of images via onError

5.1k views Asked by At

When using JavaScript and HTML why wont this work to change the class of an image if it is from a broken url?

onError="this.style.display=\'none\';this.class=null;"

i have also tried this:

onError="this.style.display=\'none\';class=null;"

and

onError="this.style.display=\'none\';class='';"

what could work?

2

There are 2 answers

1
K K On BEST ANSWER

Try this.className instead of this.class or class .

<img src="/abc.png" class="random" onError="this.style.display='none';this.className='';" />
0
k-nut On

If you want to change the css class the property you are looking for is called className not class. So you should change that.

<img class='test' src="./lad" onError="this.style.display='none'; this.className=''">

See this fiddle for comparison: http://jsfiddle.net/awbrpn99/