Home > Uncategorized > Convert text to a HTML table

Convert text to a HTML table

 Here is a handy litte script that converts tabulated text into a HTML table. Apply a CSS stylesheet to it, and it’ll look nice too …

  <textarea id="input" cols=100 rows=10>
  </textarea><br>
  <input type="button" id="Tabelize" onclick="tabilize()" value="tabilize">
  <div id="output"></div>

<script language="javascript">
 function tabilize()
 {
	var input = document.getElementById("input");
	var output = document.getElementById("output");	
	var splitInput = input.value.split("\n");
	var html = "<table>";
	for(i in splitInput)
	{
	   if (i % 2 == 0)
	   {
			html += "<tr>";
	   }
	   else
	   {
	        html += "<tr>";
	   }
	   var splitRow = splitInput[i].split("\t");
	   for(j in splitRow)
	   {
	     html += "<td>" + splitRow[j] + "</td>";
	   }
       html += "</tr>";
	}
    html += "</table>";
	html = html.replace(/</g,"&lt;");
    html = html.replace(/>/g,"&gt;"); 
	output.innerHTML = html;
 }
</script>
Advertisement
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: