Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript jQuery Basics (2014) Creating a Simple Lightbox Perfect

Robert Leonardi
Robert Leonardi
17,151 Points

Perfecting Simple lightbox. Can anybody check my code whether it is best practice or not?

I have upgraded it so that it have prev and next button, almost like carousel ? this is the app.js

var $overlay = $('<div id="overlay"></div>'); // it's JQUERY REPRESENTATION OF OBJECT makanya pake $ dan $();
var $prevImage = $('<a href="#"><img id="prevImage" src="images/chevron_left1600.png" width="50"></a>');
var $prevHref = "";
$overlay.append($prevImage);
var $image = $("<img>");
$overlay.append($image);
var $nextImage = $('<a href="#"><img id="nextImage" src="images/chevron_right1600.png" width="50"></a>');
var $nextHref = "";
$overlay.append($nextImage);
var $caption = $('<p></p>')
$overlay.append($caption);

// DON'T ADD DIV nya di html, tp di js nya aja
$("body").append($overlay);

// MAKING THE OVERLAY FUNCTION
var displayoverlay = function(ahref){
    var href = ahref.attr("href");
    var captionText = ahref.children("img").attr("alt");
    $image.attr("src",href);
    $caption.text(captionText);
    $overlay.fadeIn(300);
};

$("#imageGallery a").click(function(event){
            // stop event behaviour...
    event.preventDefault();
    displayoverlay($(this));
            // var href = $(this).attr("href");
            // var captionText = $(this).children("img").attr("alt");
            // $image.attr("src",href);
            // // $image.hide();
            // $caption.text(captionText);
            // $overlay.fadeIn(300);

});
$prevImage.click(function(){
    // SETTING PREVHREF TO PREV IMAGE A
    $prevHref = $('#imageGallery [href="'+$prevImage.next().attr('src')+'"]').parents("li").prev().children("a");
    if($prevHref.length == 0){
        // IF REACH FIRST IMAGE , WANT TO GO TO PREV, IT WILL GO TO THE LAST IMAGE
        $prevHref = $('#imageGallery [href="'+$prevImage.next().attr('src')+'"]').parents("ul").children().last("li").children("a");
    }
});
$nextImage.click(function(){
    // SETTING NEXTHREF TO NEXT IMAGE A
    $nextHref = $('#imageGallery [href="'+$nextImage.prev().attr('src')+'"]').parents("li").next().children("a");
    if($nextHref.length == 0){
        // IF REACH LAST IMAGE , WANT TO GO TO NEXT, IT WILL GO TO THE FIRST IMAGE
        $nextHref = $('#imageGallery [href="'+$nextImage.prev().attr('src')+'"]').parents("ul").children().first("li").children("a");
    }
});

$overlay.click(function(){
    // IF NEXTHREF PIC IS PRESSED
    if($nextHref != ""){
        $overlay.hide();
        displayoverlay($nextHref);
        $nextHref="";
    }
    // IF PREVHREF PIC IS PRESSED
    else if($prevHref != ""){
        $overlay.hide();
        displayoverlay($prevHref);
        $prevHref="";
    }
    // IF OTHERS ARE PRESSED
    else{
        $overlay.fadeOut(300);
    }
});

3 Answers

Ashley Carpenter
Ashley Carpenter
13,393 Points

Hi Robert,

You code looks solid, and there's more than 1 way to skin a cat so if writing it this way works for you, then that's best. If/when you get a job writing JavaScript (if you don't already) most companies have their own standards for. They're generally pretty similar but they'll teach you that in you induction period.

The only thing I would suggest if trying to get out of the habit of using the var keyword. Try and use let and const if possible as they are more reliable. They were new in ES2015 and in the projects I've worked on recently, they are fast becoming the stand.

Good job. Ashley

Ashley Carpenter
Ashley Carpenter
13,393 Points

I hope that helped Robert. If you have any other questions just let me know.

Please would you be so kind as to mark mine as best answer?

Robert Leonardi
Robert Leonardi
17,151 Points

Yeah, I wanna do that when I replied thanks, but couldn't find one,

Now i found it and just did it.

Ashley Carpenter
Ashley Carpenter
13,393 Points

Sorry, it's because I put my answer as a comment. I changed it when I realised,

Thanks very much, really appreciate it.