i have a jquery code for check/uncheck and enable/disable checkboxes, but the code is not very well written, because i have to copy and paste the same code to differentiate the checkboxes at different div container, is there any way to make the code can be called with one function?, i'm still new in jquery so i'm blind with keeping the jquery code very well/efficient written or dry, here's the jquery code:
//for container sales toggle show/hide
$('#show_hide_sales').click(function (e) {
$('#show_hide_sales').closest("#sales").find(".row:gt(0)").slideToggle("fast");
var val = $(this).text() == "-" ? "+" : "-";
$(this).hide().text(val).fadeIn("fast");
e.preventDefault();
});
$('#sales').each(function(){//loop through each container,must have different name of classes or id
$('#sales').find ($('#row_sales:first')).each(function(){ // get first row (header), need different row id name for this one line
$('#title_sales').click(function(){ //if text header clicked,must have different name of classes or id
var cur = $(this).closest('.row').closest('#sales').find(".row:gt(0)").find('div:eq(1)').find('input'); //get first column checkboxes
if(!$(cur).prop('checked')){
$(cur).closest('.row').find('div:gt(1) input').prop("disabled", false);
$("input[id^='auth2']").each(function(){
$(this).prop("checked",true);
});
}else{
$(cur).closest('.row').find('div:gt(1) input').prop("disabled", true);
$("input[id^='auth2']").each(function(){
$(this).prop("checked",false);
});
}
});
$('#sales_add').click(function(){//must have different name of classes or id for add, edit, and delete
var cur = $(this).closest('#sales').find(".row:gt(0)").find('div:eq(2)').find('input');//get first column checkboxes
if(!$(cur).prop('checked')){
$("input[id^='addAuth2']").each(function(){
$(this).prop("checked",true);
});
}else{
$("input[id^='addAuth2']").each(function(){
$(this).prop("checked",false);
});
}
});
$('#sales_edit').click(function(){
var cur = $(this).closest('#sales').find(".row:gt(0)").find('div:eq(3)').find('input');//get first column checkboxes
if(!$(cur).prop('checked')){
$("input[id^='editAuth2']").each(function(){
$(this).prop("checked",true);
});
}else{
$("input[id^='editAuth2']").each(function(){
$(this).prop("checked",false);
});
}
});
$('#sales_del').click(function(){
var cur = $(this).closest('#sales').find(".row:gt(0)").find('div:eq(4)').find('input');//get first column checkboxes
if(!$(cur).prop('checked')){
$("input[id^='delAuth2']").each(function(){
$(this).prop("checked",true);
});
}else{
$("input[id^='delAuth2']").each(function(){
$(this).prop("checked",false);
});
}
});
});
});
//for container inventory toggle show/hide
$('#show_hide_inventory').click(function (e) {
$('#show_hide_inventory').closest("#inventory").find(".row:gt(0)").slideToggle("fast");
var val = $(this).text() == "-" ? "+" : "-";
$(this).hide().text(val).fadeIn("fast");
e.preventDefault();
});
$('#inventory').each(function(){//loop through each container,must have different name of classes or id
$('#inventory').find ($('#row_inventory:first')).each(function(){ // get first row (header), need different row id name for this one line
$('#title_inventory').click(function(){ //if text header clicked,must have different name of classes or id
var cur = $(this).closest('.row').closest('#inventory').find(".row:gt(0)").find('div:eq(1)').find('input'); //get first column checkboxes
if(!$(cur).prop('checked')){
$(cur).closest('.row').find('div:gt(1) input').prop("disabled", false);
$("input[id^='auth3']").each(function(){
$(this).prop("checked",true);
});
}else{
$(cur).closest('.row').find('div:gt(1) input').prop("disabled", true);
$("input[id^='auth3']").each(function(){
$(this).prop("checked",false);
});
}
});
$('#inventory_add').click(function(){//must have different name of classes or id for add, edit, and delete
var cur = $(this).closest('#inventory').find(".row:gt(0)").find('div:eq(2)').find('input');//get first column checkboxes
if(!$(cur).prop('checked')){
$("input[id^='addAuth3']").each(function(){
$(this).prop("checked",true);
});
}else{
$("input[id^='addAuth3']").each(function(){
$(this).prop("checked",false);
});
}
});
$('#inventory_edit').click(function(){
var cur = $(this).closest('#inventory').find(".row:gt(0)").find('div:eq(3)').find('input');//get first column checkboxes
if(!$(cur).prop('checked')){
$("input[id^='editAuth3']").each(function(){
$(this).prop("checked",true);
});
}else{
$("input[id^='editAuth3']").each(function(){
$(this).prop("checked",false);
});
}
});
$('#inventory_del').click(function(){
var cur = $(this).closest('#inventory').find(".row:gt(0)").find('div:eq(4)').find('input');//get first column checkboxes
if(!$(cur).prop('checked')){
$("input[id^='delAuth3']").each(function(){
$(this).prop("checked",true);
});
}else{
$("input[id^='delAuth3']").each(function(){
$(this).prop("checked",false);
});
}
});
});
});
Aucun commentaire:
Enregistrer un commentaire