1

Need To Add/Remove Class from TD of table, based on check-box selection. By Default all the TD will be hidden. While selecting check-box, i need to enable the respective column alone.

JSFiddle : http://jsfiddle.net/eVj8V/2/

Code:

<html>

<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>

    <style>
        .hideThis {display: none;}
        td, th {
                    border: thin solid;
            }
    </style>

    <script>

        function constrctTable() {
            var TableRef = document.getElementById("TableConstruction");
            TableRef.innerHTML = "";
            var table = "";
            table += "<table>";
            table += "<tr style='border: inherit;' id='tableColumns'>";
            table += "<td>S.No</td>";
            table += "<td>Name</td>";
            table += "<td class=hideThis>Employee No</td>";
            table += "<td class=hideThis>Manager No</td>";
            table += "<td class=hideThis>Clerk No</td>";


            table += "</tr>";
            for(i=0; i<5; i++) {
                table += "<tr class=recordRow>";
                table += "<td>"+i+"</td>";
                table += "<td>Raj "+i+"</td>";
                table += "<td class=hideThis name='Employee'>"+ i +"</td>";
                table += "<td class=hideThis name='Manager'>"+ i +"</td>";
                table += "<td class=hideThis name='Clerk'>"+ i +"</td>";
                    table += "</tr>";
            }
            table += "</table>";

            TableRef.innerHTML = table;
        }


        function enableEmployee(enableRef) {
            var compRef = document.getElementById('employee').checked;
            if(compRef){
                $('.hideThis:contains(' + enableRef + ')').removeClass('hideThis');
                $('.recordRow td.hideThis[name=' +enableRef+ ']').removeClass('hideThis');
            } else {
                $('.hideThis:contains(' + enableRef + ')').addClass('hideThis');
                $('.recordRow td.hideThis[name !=' +enableRef+ ']').addClass('hideThis'); 
            }
        }


        function enableManager(enableRef) {
            var compRef = document.getElementById('manager').checked;
            if(compRef){
                $('.hideThis:contains(' + enableRef + ')').removeClass('hideThis');
                $('.recordRow td.hideThis[name=' +enableRef+ ']').removeClass('hideThis');
            } else {
                $('.hideThis:contains(' + enableRef + ')').addClass('hideThis');
                $('.recordRow td.hideThis[name !=' +enableRef+ ']').addClass('hideThis'); 
            }
        }

        function enableClerk(enableRef) {
            var compRef = document.getElementById('clerk').checked;
            if(compRef){
                $('.hideThis:contains(' + enableRef + ')').removeClass('hideThis');
                $('.recordRow td.hideThis[name=' +enableRef+ ']').removeClass('hideThis');
            } else {
                $('.hideThis:contains(' + enableRef + ')').addClass('hideThis');
                $('.recordRow td.hideThis[name !=' +enableRef+ ']').addClass('hideThis'); 
            }
        }

    </script>

</head>

<body onload="constrctTable();">

<div id="TableConstruction"> </div>

<input type='checkbox'  id='employee' onclick='enableEmployee("Employee")'>Employee</input>
<input type='checkbox'  id='manager' onclick='enableManager("Manager")'>Manager</input>
<input type='checkbox'  id='clerk'  onclick='enableClerk("Clerk")'>Clerk</input>

</body>


</html>

Thanks in advance.

2
  • 1
    Do you want them to be hidden on respective checkbox uncheck. Commented Mar 12, 2014 at 7:32
  • yes, by default it will be hidden... while check the check-box it will display its corresponding column... but while unchecking it is not hiding as expected @Milind Anantwar. Commented Mar 12, 2014 at 7:40

1 Answer 1

2

I have modified your code to make it simple.Try this.

$(document).on('change','input[type="checkbox"]',function(){
var compRef = this.checked;
if(compRef){
  $("tr td:contains('"+$(this).attr('name')+"')").removeClass('hideThis');
  $("tr td[name='"+$(this).attr('name')+"']").removeClass('hideThis');
}else{
  $("tr td:contains('"+$(this).attr('name')+"')").addClass('hideThis') 
  $("tr td[name='"+$(this).attr('name')+"']").addClass('hideThis');
}});

Working Demo

Sign up to request clarification or add additional context in comments.

4 Comments

change event is not firing in my browser. im using FF of version 27.0.1
How to achieve it using click event... If i place the if condition in click event function. it not responding as expected.
on else condition i used $('*[class=""]').addClass('hideThis'); but still it remove all the td
You need to use .live instead of .on in that case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.