how to merge checked column using js or jquery/

Hi, I created a table with three rows and 4 column and I have a checkbox for each row.
like this
Then if I click the merge button the row which is checked are to be merged
<html><head>
<script>

function redclr(){
 	for (var i = 1; i <= 3; i++) 
 	{
    	var lfckv = document.getElementById("lifecheck"+i).checked;
    	if (lfckv == true) {
			document.getElementById("row"+i).style.backgroundColor="red";
			var tds = document.getElementsByTagName("td");
        	var firstCellId = "";
        	var mergedCells = "";
        for (var i = 0; i < tds.length; i++) {
                mergedCells += tds[i].textContent;
                if (firstCellId == "") {
                    firstCellId = tds[i].id;
                }
                else {
                    tds[i].style.backgroundColor = "gray";
                    tds[i].style.display = "none";
                    tds[i].textContent = "";
                }
        }
        var firstCell = document.getElementById(firstCellId);
        firstCell.textContent = mergedCells;
        firstCell.style.backgroundColor = "gray";
		}
	}
}

</script>
<style>
table,tr,td{ border:1px solid;
             width:200px;
             hieght:150px;
             border-collapse:collapse;
}
           
</style>
</head>
<body>
<table id="demo">

<tbody>
<tr id="row1">
<td id="sel1">a<input type="checkbox" id="lifecheck1"></td>
<td id="sel2">b</td>
<td id="sel3">c</td>
</tr>

<tr id="row2">
<td id="sel4">d<input type="checkbox" id="lifecheck2"></td>
<td id="sel5">e</td>
<td id="sel6">f</td>

</tr>

<tr id="row3">
<td id="sel7">g<input type="checkbox" id="lifecheck3"></td>
<td id="sel8">h</td>
<td id="sel9">i</td>
</tr>

</tbody></table>

<button type="button" onclick="redclr()">merge</button> 
 




</body></html>

Open in new window


For example: if I selected 1st and 2nd checkbox then first two row need too merge .
Ajith KumarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MishaProgrammerCommented:
Can you put your code here?
1
Chris StanyonWebDevCommented:
Your code looks a little confusing so it's not clear exactly what you need merging and where you want the merged results to go. You've tagged this as jQuery, so I'm guessing you want a jQuery solution. Take a look at the following:

//HTML

<table id="table">
    <tr>
        <td><input type="checkbox"></td>
        <td>a</td>
        <td>b</td>
        <td>c</td>
    </tr>
    <tr>
        <td><input type="checkbox"></td>
        <td>d</td>
        <td>e</td>
        <td>f</td>
    </tr>
    <tr>
        <td><input type="checkbox"></td>
        <td>g</td>
        <td>h</td>
        <td>i</td>
    </tr>
</table>

<button id="merge">merge</button>

Open in new window

// jQuery

$(document).ready(function() {
    $('#merge').click(function(e) {
        $('#table tr').each(function(i, row) {
            var text = "";
 
            if ($(row).find('input').is(":checked")) {
                $(row).css('background-color', 'red');
 
                $(row).find('td').each(function() {
                    text += $(this).text();
                });
 
                $('td:first-child', row).text(text);
            }
        })
    });
});

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MishaProgrammerCommented:
There is no activity from author.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.