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>
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>