twitterfacebookgoogle plusrss feed

Pages

Sunday, July 1, 2012

JQuery checkboxes implementation


I found many queries about "how we will make multiple check/unchecked button using JQuery, as well as how to count selected check boxes and then delete the selected records?". Its too easy!

Before working on this example I want to say, always follow the documentation of the product because you will found answers for many questions. Searching on internet not just take your a lot of time but you make yourself addicted of copy and paste.


$(function() {
    $("input[rel=main]").click(function() {
        $("input[rel=records]").attr("checked", this.checked);
        var c = $("input[rel=records]:checked").length;
        $("div > span").text((c > 1) ? c + " records are selected" : c + " record selected");
    });

    $("input[rel=records]").click(function() {
        var c = $("input[rel=records]:checked").length;
        $("div > span").text((c > 1) ? c + " records are selected" : c + " record selected");

        if ($("input[rel=records]").length == $("input[rel=records]:checked").length) {
            $("input[rel=main]").attr("checked", "checked");
        } else {
            $("input[rel=main]").removeAttr("checked");
        }
    });

    $("input[type=button]").click(function() {
        var n = $("input[rel=records]:checked").length;
        if (n <= 0) {
            alert("Select at least one record to delete");
        } else if (n >= 1) {
            var answer = confirm("Are you sure you want to delete " + n + " record(s)?");
            if (answer) {
                $("input[rel=records]:checked").parent().hide();
                $("input[type=button]").after((n > 1) ? " " + n + " records are deleted :)" : " " + n + " record deleted :)");
                $("input:checkbox").removeAttr("checked");
                $("div > span").text("");
            }
        }
    });
});

0 comments:

Post a Comment

comment or ask