You must Sign In to post a response.
  • Category: JQuery

    Each function erro in jquery

    Hi ,

    in my application data will display in gridview, using jquery function i have checked each cell.and add an anchor tag with an id inside of each cell.

    following is working code
    var firstcelltext;



    $("#gridtable tr td").filter(":not(td:first-child)").each(function () {

    firstcelltext = $(this).parent().find("td:first-child").text();

    var fcelltext = firstcelltext.replace(/ /g, '-');

    var colindex = $(this).closest("tr td").prevAll("tr td").length;

    var headertext = $("table[id*=gridtable] th").eq(colindex).text();

    var cellText = $(this).text();

    if ($.trim(cellText) == '') {

    $(this).css('background-color', 'Red');



    $(this).append("<a class='anchor1' id='" + headertext + "-" + fcelltext + "' href='#'>N</a>");



    }

    else {







    $(this).text('');

    $(this).append("<a class='anchor2' id='" + headertext + "-" + fcelltext + "' href='#' >Y</a>");

    $(this).css('background-color', 'LightGreen');



    }

    });

    each cell rendered like this

    <a id="11-01-Jan-2015" class="anchor1" href="#">N</a>. this is fine for my application.

    my requirement is i want to use above code in html table.

    i tried same code but cell is rendered like this

    <a id="11 -01-Jan-2015-" class="anchor1" href="#">N</a>

    there is a slight difference. in second output. an extra hyphen is added and a space between 11 and 01 as it rendered .

    another problem is following code working fine in gridview but not in table
    $(' .anchor1').click(function () {



    alert("test");



    });

    how to solve both problems.

    Regards

    Baiju
  • #755157
    you have the error in the particular line for replace with the line

    var fcelltext=firstcelltext.replace(//g,'-')

    better checkit out with checkpoint

  • #757560
    Hi,

    it is problem with replace function may be just try like this

    var firstcelltext;



    $("#gridtable tr td").filter(":not(td:first-child)").each(function () {

    firstcelltext = $(this).parent().find("td:first-child").text();

    var fcelltext = firstcelltext.replace('/g', '-');

    var colindex = $(this).closest("tr td").prevAll("tr td").length;

    var headertext = $("table[id*=gridtable] th").eq(colindex).text();

    var cellText = $(this).text();

    if ($.trim(cellText) == '') {

    $(this).css('background-color', 'Red');



    $(this).append("<a class='anchor1' id='" + headertext + "-" + fcelltext + "' href='#'>N</a>");



    }

    else {







    $(this).text('');

    $(this).append("<a class='anchor2' id='" + headertext + "-" + fcelltext + "' href='#' >Y</a>");

    $(this).css('background-color', 'LightGreen');



    }

    });


  • Sign In to post your comments