This piece of code swops between 2 background images. It currently works in Google Chrome and Safari, but not Firefox or IE. I am a noob with JQuery (this is my first time using it :D) so please help.
Also it would be nice if I could swop between images by only pressing 1 button, not 2 separate ones like now. But I really for now just want it working in all browsers :)
$(document).ready(function(){ // image array var imgs = ["banner1.jpg", "banner2.jpg"]; // set init image $("#image").css("background-image", "url('images/" + imgs[0] + "')"); $("#image").attr("rel", 0); // handle user key press $(this).keypress(function (e) { var keynum = ""; if (window.event) { keynum = e.keyCode; } else if (e.which) { keynum = e.which; } // if the enter key has been pressed if (keynum == 13) { // display next image in array or start from the beginning var current = $("#image").attr("rel"); if (current == (imgs.length - 1)) { current = 0; } else { current++; } // change image $("#image").css("background-image", "url('images/" + imgs[current] + "')"); $("#image").attr("rel", current); } }); });
This piece of code swops between 2 background images. It currently works in Google Chrome and Safari, but not Firefox or IE. I am a noob with JQuery (this is my first time using it :D) so please help.
Also it would be nice if I could swop between images by only pressing 1 button, not 2 separate ones like now. But I really for now just want it working in all browsers :)
Thank You!
This should work across all browsers.