Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Programming

    I want to change the content of <TD> tag of a HTML table sorted by alphabets(A-Z) in a different HTML table using javascript?

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    I want to change the content of <TD> tag of a HTML table sorted by alphabets(A-Z) in a different HTML table using javascript?
    Please post your current code (wrap your code by [CODE][/CODE] tags while posting here) and explain your requirement/issue on the basis of that.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    Here ya go:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Test</title>
    <script type="text/javascript">
    // <![CDATA[
    
    
    
    function sortAndCopyTable(index)
    	{
    	/* order rows by column index */
    	var old_table = document.getElementById("table_1");
    	var trs = old_table.getElementsByTagName("tbody")[0].getElementsByTagName("tr");
    	var rows = new Array();
    	var keys = new Array();
    	var xhtml_tag_regexp = new RegExp("<[^>]*>","g");
    	for (var i=0;i<trs.length;i++)
    		{
    		var tds = trs[i].getElementsByTagName("td");
    		var row = new Array();
    		for (var c=0;c<tds.length;c++) row[c] = tds[c].innerHTML;
    		rows[i] = row;
    		keys[i] = [(row[index]+"").replace(xhtml_tag_regexp,""),i];
    		}
    	keys.sort();
    	var ordered_rows = new Array();
    	for (var i=0;i<keys.length;i++) ordered_rows[keys[i][1]] = rows[i];
    
    
    	/* insert sorted data into new table */
    	var new_table = document.getElementById("table_2");
    	var new_table_tb = new_table.getElementsByTagName("tbody")[0];
    	new_table.getElementsByTagName("thead")[0].innerHTML = old_table.getElementsByTagName("thead")[0].innerHTML;
    	var new_html = "";
    	for (var i=0;i<ordered_rows.length;i++)
    		{
    		new_html += "<tr>";
    		for (var c=0;c<ordered_rows[i].length;c++)
    			{
    			new_html += "<td>";
    			new_html += ordered_rows[i][c];
    			new_html += "</td>";
    			}
    		new_html += "</tr>";
    		}
    	new_table_tb.innerHTML = new_html;
    	}
    
    // ]]>
    </script>
    </head>
    <body>
    
    <table id="table_1">
    	<thead>
    		<tr>
    			<th>Name</th>
    			<th>Age</th>
    			<th>Gender</th>
    		</tr>
    	</thead>
    	<tbody>
    		<tr>
    			<td>Ron Jon</td>
    			<td>24</td>
    			<td>M</td>
    		</tr>
    		<tr>
    			<td>Richard Cory</td>
    			<td>32</td>
    			<td>M</td>
    		</tr>
    		<tr>
    			<td>Shirley Temple</td>
    			<td>17</td>
    			<td>F</td>
    		</tr>
    		<tr>
    			<td>Bill Smithers</td>
    			<td>46</td>
    			<td>M</td>
    		</tr>
    		<tr>
    			<td>Richard Cory</td>
    			<td>41</td>
    			<td>M</td>
    		</tr>
    	</tbody>
    </table>
    
    <br />
    <input type="button" value="sort by name" onclick="sortAndCopyTable(0)" />
    <br /><br />
    
    <table id="table_2">
    	<thead></thead>
    	<tbody></tbody>
    </table>
    
    </body>
    </html>
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •