/    Sign up×
Community /Pin to ProfileBookmark

how can i delete a text box dynamically

how can i delete a dynamically generated textarea by pressing on a button?.the textarea is generated by the following javascript.
<html>
<head>
<script language=”javascript”>
function genBox(n)
{
var thebox = document.createElement(‘textarea’);
thebox.setAttribute(‘rows’,’10’);
thebox.setAttribute(‘cols’,’30’);
document.getElementById(n).appendChild(thebox);

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

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Mr_JNov 18.2006 — Try something like this

[code=php]<html>
<head>
<script language="javascript">
function genBox(n){
var thebox = document.createElement('textarea');
thebox.setAttribute("id","t1")
thebox.setAttribute('rows','10');
thebox.setAttribute('cols','30');
document.getElementById(n).appendChild(thebox);
}

function removeBox(id){
el=document.getElementById(id)
document.getElementById("boxhold1").removeChild(el)
}
</script>
</head>
<body>
<input name="Input" type=button onclick="genBox('boxhold1')" value="insert textarea" />
<div id="boxhold1"></div>

<input type=button onclick="removeBox('t1')" value="Remove Textarea" />
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@ZnupiNov 18.2006 — Give your text box an id and use
<i>
</i>var toRemove = document.getElementById('textBoxId');
document.getElementById(n).removeChild(toRemove);


Edit: Mr J, you beat me to it ?
Copy linkTweet thisAlerts:
@mrhooNov 18.2006 — I would use the same button to create or destroy the textarea:

[CODE]function genBox(n){
var who= document.getElementById(n);
var btn= document.getElementsByName('Input')[0];
if(who){
who.parentNode.removeChild(who);
btn.value="insert textarea";
}
else{
var thebox = document.createElement('textarea');
thebox.setAttribute('rows','10');
thebox.setAttribute('cols','30');
document.getElementById(n).appendChild(thebox);
btn.value="Remove textarea";
}

}[/CODE]
Copy linkTweet thisAlerts:
@Mr_JNov 19.2006 — [B]forjavascript[/B] Re: pm

Try this

[code=php]<html>
<head>
<script type="text/javascript">

function genBox(id){

if(!document.getElementById("textboxid")){

var thebox = document.createElement('textarea');
thebox.setAttribute('id','textBoxId');
thebox.setAttribute('rows','10');
thebox.setAttribute('cols','30');

document.getElementById(id).appendChild(thebox);

var but=document.createElement('input');
but.setAttribute('type','button');
but.setAttribute('value','Remove Textarea');

but.onclick=function(){
document.getElementById(id).removeChild(thebox);
document.getElementById(id).removeChild(but);
document.getElementById(id).removeChild(newbr);
}

newbr=document.createElement('BR');
document.getElementById(id).appendChild(newbr);
document.getElementById(id).appendChild(but);

}

}

</script>
</head>
<body>

<input name="Input" type=button onclick="genBox('boxhold1')" value="Insert Textarea" />
<div id="boxhold1"></div>

</body>
</html>
[/code]
×

Success!

Help @forjavascript spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...