Preserve blank lines when add multiple lines of text to a cell

C

Cah Sableng

I have a function which adds multiple lines from a textarea to a cell
within table. Code follow. It works fine for me, except for blank
lines. The blank lines become stripped. How to preserve those blank
lines?

TIA.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html>
<head>
<title>Add Row (Multiple Line)</title>
<script type="text/javascript">
function addRow(idTbl)
{
var frm,tbl,re,oldStr,newRow,newCell,matched;
frm = document.forms['formMain'];
tbl = document.getElementById(idTbl);
re = /([^\r\n]*)([\r\n]+)/;
oldStr = frm.elements['taDesc'].value;

newRow = tbl.tBodies[0].insertRow(-1);
newCell = newRow.insertCell(-1);
while (re.test(oldStr))
{
matched = re.exec(oldStr);
oldStr = oldStr.replace(re, "");
newCell.appendChild(document.createTextNode(matched[1]));
newCell.appendChild(document.createElement('br'));
//alert("'" + matched[2] + "'");
}
newCell.appendChild(document.createTextNode(oldStr));
}
</script>
<style type="text/css">
td {
padding:2px 5px;
border:1px solid black;
}
</style>
</head>

<body>
<form name="formMain">
<textarea rows="4" cols="30" name="taDesc">Line 1
Line 2
Line 3</textarea><br>
<input type="button" name="add" value="Add" onclick="addRow('tblA');">
</form>
<table id="tblA">
<tbody>
</tbody>
</table>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top