Forums

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

Home Forums JavaScript Annoying jQuery Issue

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #38426
    Roxon
    Member

    I am creating a custom jQuery slideshow with dynamically generated pagination. There is a #slides div containing all the images. When the page is loaded, the following is ran:

    $("#slides img").each(function(index) {
    $(this).attr("id", index); // Image class = index (0, 1, 2 etc)
    slideCount++;
    // Generate Pagination and CSS
    if (index != 0) {
    $("#pagination").append("Navigate to " + index + "");
    $(this).css("display", "none");
    } else {
    $("#pagination").append("Navigate to " + index + "");
    }
    });

    So, a generated pagination item looks like this:

    Navigate to 1

    When you click an arrow to navigate through slides, jQuery throws an exception when this code is ran:

    ("#p" + currentSlide).attr("src", "img/dot-empty.png");

    The error generated says “Object #p1 has no method ‘attr’.” (#p1 is just an example, it could be #p5 or #p300). I can’t for the life of me understand why. The incredibly frustrating aspect is that when I type the command into the console, it runs perfectly. I just can’t run it through the script.

    #104137
    Mottie
    Member

    It is just a typo, or is that really missing a “$”?

    $("#p" + currentSlide).attr("src", "img/dot-empty.png");

    If that was just a typo, then add a check to make sure the element exists, like this:

    var p = $("#p" + currentSlide);
    if (p.length){
    p.attr("src", "img/dot-empty.png");
    }
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.