PDA

View Full Version : can you help me to write code for "remove textarea"



forjavascript
11-19-2006, 11:10 AM
can you help me to write code for "remove textarea" button?
<html>
<head>
<script language="javascript">
function genBox(n)
{
var thebox = document.createElement('textarea');
thebox.setAttribute('rows','10');
thebox.setAttribute('cols','30');
thebox.setAttribute('id','textBoxId');
document.getElementById(n).appendChild(thebox);
var but=document.createElement('input');
but.setAttribute('type','button');
but.setAttribute('value','remove textarea');
but.setAttribute('name','remove');
document.getElementById('boxhold1').appendChild(but);

}
</script>
</head>
<body>
<input name="Input" type=button onclick="genBox('boxhold1')" value="insert textarea" />
<div id="boxhold1"></div>
</body>
</html>

DimX
11-19-2006, 12:19 PM
<html>
<head>
<script language="javascript">
function genBox(n)
{
var thebox = document.createElement('textarea');
thebox.setAttribute('rows','10');
thebox.setAttribute('cols','30');
thebox.setAttribute('id','textBoxId');
document.getElementById(n).appendChild(thebox);
var but=document.createElement('input');
but.setAttribute('type','button');
but.setAttribute('value','remove textarea');
but.setAttribute('name','remove');
but.onclick = function() {
this.previousSibling.removeNode(true);
this.removeNode(true);
};
document.getElementById('boxhold1').appendChild(but);
}
</script>
</head>
<body>
<input name="Input" type=button onclick="genBox('boxhold1')" value="insert textarea" />
<div id="boxhold1"></div>
</body>
</html>

Same ID for all textareas is not good though.

Btw. it only works if there's nothing between the textarea and the remove button, else IDs should be used.

coothead
11-20-2006, 10:44 AM
Hi there DimX,

are you aware that removeNode will not work in Firefox?

forjavascript, you may see a crossbrowser solution here...

http://www.codingforums.com/showpost.php?p=506611&postcount=2

coothead