Click here to Skip to main content
15,891,850 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friends, I need your help please!
I'm building javascript with html Crud,and trying to heighlight the row when check the checkbox. it colors just the first column- the Header !
plz help.
or maybe someone have something-code equals in html and javascript..

here is my code:

XML
<HTML>
<HEAD>
    <TITLE> Add/Remove dynamic rows in HTML table </TITLE>

<link rel="stylesheet" type="text/css" href="style.css">

    <SCRIPT language="javascript">
      function addRow(tableID) {

            var table = document.getElementById(tableID);

            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var colCount = table.rows[0].cells.length;

            for(var i=0; i<colCount; i++) {

                var newcell = row.insertCell(i);

                newcell.innerHTML = table.rows[0].cells[i].innerHTML;

                //alert(newcell.childNodes);
                switch(newcell.childNodes[0].type) {


                    case "checkbox":
                            newcell.childNodes[0].checked = false;
                            break;
                    case "select-one":
                            newcell.childNodes[0].selectedIndex = 0;
                            break;
                }
            }
        }


        function deleteRow(tableID) {
            try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox && true == chkbox.checked) {
                    if(rowCount <= 1) {
                        alert("Cannot delete all the rows.");
                        break;
                    }
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                  }
        }
            }
        catch(e) {
                    alert(e);
             }
        }




// when checkbox is checked.


function highlight(box, g)
{
     var color1 = 'yellow';
     var color2 = '';

     document.getElementById(g).style.background = (box.checked ? color1 : color2);
}

    </SCRIPT>

</HEAD>




<BODY>

<div>

    <TABLE id="dataTable">
        <tr id="g">
         <TD><INPUT type="checkbox" name="chk" onClick="highlight(this,'g')" /></TD>
         <th id="fName">First Name</th>
         <th>Last Name</th>
        </tr>

    </TABLE>
    <INPUT type="button" value="Delete Checked People" class="btnDelete" onclick="deleteRow('dataTable')" />
    <INPUT type="button" value="Add Person" class="btnAdd" onclick="addRow('dataTable')" />


</div>

</BODY>
</HTML>
Posted
Updated 2-Dec-14 4:58am
v2

I have checked your code. Your code highlight the first row only because it gets the row with same ID='g'. And ID is always unique and can't be used more than once in same page.
The other rows which are dynamically generating on "Add Person" button click have no ID.
The rows are generating in this way.

XML
<table id="dataTable">
        <tbody><tr id="g">
         <td><input type="checkbox" name="chk" onclick="highlight(this,'g')"></td>
         <th id="fName">First Name</th>
         <th>Last Name</th>
        </tr>

    <tr><td><input type="checkbox" name="chk" onclick="highlight(this,'g')"></td><td>First Name</td><td>Last Name</td></tr><tr><td><input type="checkbox" name="chk" onclick="highlight(this,'g')"></td><td>First Name</td><td>Last Name</td></tr><tr><td><input type="checkbox" name="chk" onclick="highlight(this,'g')"></td><td>First Name</td><td>Last Name</td></tr></tbody></table>


You can check that the page rendered in this way that No ID has been assigned to other table rows. Provide ID dynamically to rows when they are created. You can refer these links:
http://stackoverflow.com/questions/19625646/javascript-adding-an-id-attribute-to-another-created-element[^]

http://www.dustindiaz.com/add-and-remove-html-elements-dynamically-with-javascript/[^]
 
Share this answer
 
Hi,

I have made few changes to your code. Introduced onclick event handler to the checkbox.

-------------------------------------------------------
HTML
<HTML>
<HEAD>
    <TITLE> Add/Remove dynamic rows in HTML table </TITLE>
 
<link rel="stylesheet" type="text/css" href="style.css">
 
    <SCRIPT language="javascript">
      function addRow(tableID) {
            var table = document.getElementById(tableID);
			var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
			var colCount = table.rows[0].cells.length;
			for(var i=0; i < colCount; i++) {
				var newcell = row.insertCell(i), cellNode =null;
				newcell.innerHTML = table.rows[0].cells[i].innerHTML;
				cellNode = newcell.childNodes[0];
				switch(cellNode.type) {
					case "checkbox":
							cellNode.checked = false;
							cellNode.id = "checkBox_"+i;
							cellNode.onclick = function (node) {
									return function () {
										highlight(node,row);
									};
							}(cellNode);
                            break;
                    case "select-one":
                            cellNode.selectedIndex = 0;
                            break;
                }
            }
        }
 

        function deleteRow(tableID) {
            try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;
 
            for(var i=0; i < rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox && true == chkbox.checked) {
                    if(rowCount <= 1) {
                        alert("Cannot delete all the rows.");
                        break;
                    }
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                  }
        }
            }
        catch(e) {
                    alert(e);
             }
        }
	// when checkbox is checked.
	function highlight(checkBox,row)
	{
		 var color1 = "grey", color2 ="";
		 row.style.backgroundColor = (checkBox.checked ? color1 : color2);
	}
 
    </SCRIPT>
 
</HEAD>
<BODY>
<div>
 
    <TABLE id="dataTable" cellspacing="0" borderspacing='1' style='border: solid 1px black'>
        <tr id="g">
         <TD><INPUT type="checkbox" name="chk" id='checkBox_0' onclick="highlight(document.getElementById('checkBox_0'), document.getElementById('g'))"  /></TD>
         <th id="fName">First Name</th>
         <th>Last Name</th>
        </tr>
 
    </TABLE>
    <INPUT type="button" value="Delete Checked People" class="btnDelete" onclick="deleteRow('dataTable')" />
    <INPUT type="button" value="Add Person" class="btnAdd" onclick="addRow('dataTable')" />
</div>
 
</BODY>
</HTML>
 
Share this answer
 
v4
Comments
Member 10279189 2-Dec-14 13:05pm    
can I make changes on the Font, like Bold font and Bold border to the same row?
SrikantSahu 2-Dec-14 13:22pm    
Hi,
You can change this code in the following way..
function highlight(checkBox,row)
{
var color1 = "#CCCCCC", color2 ="";
row.style.backgroundColor = (checkBox.checked ? color1 : color2);

if(checkBox.checked) {
row.cells[1].style.fontSize="15px";
row.cells[2].style.fontSize="15px";
row.cells[1].style.fontFamily='verdana';
row.cells[2].style.fontFamily="verdana"

row.cells[1].style.fontWeight="bold"; row.cells[2].style.fontWeight="bold";
} else {
row.cells[1].style.fontSize="13px";
row.cells[2].style.fontSize="13px";
row.cells[1].style.fontFamily='verdana';
row.cells[2].style.fontFamily="verdana"

row.cells[1].style.fontWeight="normal"; row.cells[2].style.fontWeight="normal";
}

}


But it can be more effective ...



you can set color to the table also....
<TABLE id="dataTable" cellspacing="0" borderspacing='1' style='border: solid 1px black; background-color:#EFEFEF'>
Member 10279189 2-Dec-14 13:32pm    
thank you very much
Member 10279189 2-Dec-14 13:09pm    
Thank you :)
SrikantSahu 2-Dec-14 13:22pm    
you welcome :)
Here is an easy fix, it should work:
JavaScript
//function highlight(box, g)
function highlight(box)
{
     var color1 = 'yellow';
     var color2 = '';
     //document.getElementById(g).style.background = (box.checked ? color1 : color2);
	 box.parentNode.parentNode.style.background = (box.checked ? color1 : color2);
}

But take into consideration solution 1 comments, the reasoning behind why your code is not working.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900