Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript JQuery doesn’t work in Firefox or IE

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #31272
    JediN1nja
    Participant

    Hey Everyone!

    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 :)








    Escape Button






    Thank You!

    #63525
    stefanbar
    Member

    Hi JediN1nja

    This should work across 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);
    }
    });
    });
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.