Just wondered if this code could be optomiosed in anyway? It works great, just wanted to make sure there weren't any ways to simplify this or if there are any obvious errors in the markup. Asking primerly because of that second call in the first function, not sure if it's needed?

$(document).on("click", '.load', function (event) {
    var $projects = $("#projects"),
        id = $(this).data('id');
    $projects.html("").data('id', id);
    $("#loading").show();
    $projects.load("projects.html #div" + id, function () {
        $("#projects").show();
        $("#loading").hide();
    })
});

$(document).on("click", '.next', function (event) {
    var $projects = $("#projects");
    pid = $projects.data('id');
    $projects.data('id', (pid == 6) ? 1 : (pid + 1));
    $projects.load("projects.html #div" + $projects.data('id'));
});


$(document).on("click", '.previous', function (event) {
    var $projects = $("#projects");
    pid = $projects.data('id');
    $projects.data('id', (pid == 1) ? 6 : (pid - 1));
    $projects.load("projects.html #div" + $projects.data('id'));
});

$(document).on("click", '.exit', function (event) {
    $("#projects").hide();
});

Recommended Answers

All 4 Replies

The next and previous functions are similar enough to be merged into a single function.

Mind showing me how to do that? I don't really see how that's possible with it replying on click classes?

Here's a start:

function loadProject(var next) {
    var $projects = $("#projects");
    pid = $projects.data('id');

    if (next) {
        pid = (pid == 6) ? 1 : (pid + 1);
    }
    else {
        pid = (pid == 1) ? 6 : (pid - 1);
    }

    $projects.data('id', pid);
    $projects.load("projects.html #div" + pid);
}

$(document).on("click", '.next', function (event) {
    loadProject(true);
});

$(document).on("click", '.previous', function (event) {
    loadProject(false);
});

Just given that a go and having a hard time getting anyting to work if I'm honest.

I'm getting an error on this line function loadProject(var next) {

Is that just because It's rough code or am I missing something?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.