javascript - jquery toggle only execute the first condition -


this must simple many of you. wrote function select checkboxes if user clicks on anchor tag (). however, on first click function select checkboex nothing happens when click on anchor tag again (it should deselect checkboxes again). here js function

function createfooterrowforrunningdoubles(trclassname, colspannumber, maintable, table, i) {      var maintableid = $.data(maintable, 'bettabledata').tableid;     var tr = $('<tr/>')         .addclass(trclassname)         .appendto(table)         .append($('<td colspan="' + colspannumber + '"/>'))         .append($('<td />')             .append($('<a id="fieldselectall"/>')                 .html(field)                 .click(function() {                     var els = document.getelementsbyname(maintableid + "_select" + i);                     (var j = 0; j < els.length; j++) {                         if ($("#fieldselectall").toggle()) {                             els[j].checked = true;                         }                     }                 })) ) }   

edit 1:

so if remove anchor tag , replacing select check box works if modify function follow:

function createfooterrowforrunningdoubles(trclassname, colspannumber, maintable, table, i) {      var maintableid = $.data(maintable, 'bettabledata').tableid;      // create row checkbox     var tr = $('<tr/>')         .addclass(trclassname)         .appendto(table)         .append($('<td colspan="' + colspannumber + '"/>'))         // create checkbox         .append($('<td/>')             .html(field)             .append($('<input type="checkbox" id="' + maintableid + '_select' + + '"/>'))                 .click(function () {                     var els = document.getelementsbyname(maintableid + "_select" + i);                     (var j = 0; j < els.length; j++) {                         if ($("#" + maintableid + "_select" + i).is(':checked')) {                             els[j].checked = true;                         } else els[j].checked = false;                     }                 })) } 

however, instead of using 'checkall' checkbox, need toggle checkall thing on anchor tag click. if makes senses?

use variable keep track of state:

function createfooterrowforrunningdoubles(trclassname, colspannumber, maintable, table, i) {      var maintableid = $.data(maintable, 'bettabledata').tableid;     var allchecked = false;     var tr = $('<tr/>')         .addclass(trclassname)         .appendto(table)         .append($('<td colspan="' + colspannumber + '"/>'))         .append($('<td />')             .append($('<a id="fieldselectall"/>')                 .html(field)                 .click(function() {                     var els = document.getelementsbyname(maintableid + "_select" + i);                     allchecked = !allchecked;                     (var j = 0; j < els.length; j++) {                         els[j].checked = allchecked;                     }                 })) ) }   

Comments

Popular posts from this blog

hibernate - How to load global settings frequently used in application in Java -

python 3.x - Mapping specific letters onto a list of words -

objective c - Ownership modifiers with manual reference counting -