I wrote an inline script to automatically handle when a browser can not load an SVG and replace it with a PNG image. This part works great, however I also want to change the image onhover and this answer worked great for me except that I don't need it to "run and find" if the browser can handle SVG and doesn't use the PNGs. So, I thought I would have it trigger when a class of noSVG
(herein referred to as "myclass") was added to the IMG
tags.
Now here's were the issues start, I can use CSS to modify the style of the class which is added. But I can't use jQuery to modify it. Even stranger, when I went to add it to JSFiddle for you guys, it works. Before you guys think that I'm using a bad version of jQuery I tested multiple versions (including 1.10.1 which is use by JSFiddle).
HTML:
<img src="1.svg" onerror="this.onerror=null; this.src='1.png'; this.className+=' myclass';" class="image" id="1" />
jQuery:
$(function() {
$(".myclass")
.mouseover(function() {
var src = $(this).attr("src").match(/[^\.]+/) + "hover.png";
$(this).attr("src", src);
//console.log("moused over");
})
.mouseout(function() {
var src = $(this).attr("src").replace("hover.png", ".png");
$(this).attr("src", src);
//console.log("moused out");
});
});