Removing a element (DOM/JS)

J

Jam Pa

Ok I am one of those unfortunate souls who have to make a text editor for
CMS.

So, I need to remove a <textarea> on a page (this is for non javascript
browsers).

I can figure out the element (getLelementById), but so far have been unable
to remove it. I tried to set the elements innerHTML to "". Interestingly,
in firefox the element is doubled (1.0.4).

anyone have any help with this. I have ie support to work on too, so *any*
pointers for *any* browsers on replacing elements with javascript are very
very welcome.
 
M

Martin Honnen

Jam said:
So, I need to remove a <textarea> on a page (this is for non javascript
browsers).

I can figure out the element (getLelementById), but so far have been unable
to remove it.

if (element.parentNode && element.parentNode.removeChild) {
element.parentNode.removeChild(element);
}
 
J

Jam Pa

if (element.parentNode && element.parentNode.removeChild) {
element.parentNode.removeChild(element);
}

Thanks for your reply.

The code below works.. at times. And sometimes it wont. Go figure...

<div id="supa">
<p>ldakjsdlkjaslkjsad</p>
<p>etc</p>
<textarea id="tarea"></textarea>
<script language="JavaScript" type="text/javascript">
function removeElementById() {
parn = "supa";
rele = "storyarea";
pare = document.getElementById(parn);
rele = document.getElementById(rele);
fstatus = "";

if (tarea.parentNode && tarea.parentNode.removeChild
(tarea)) { tarea.parentNode.removeChild(tarea); }
else { alert("Nada!"); }
fstatus = "jaa";
return fstatus;
}
removeElementById();
</script>
</div>
 
J

Jam Pa

Ok well this is how Im working this script in 'final' (lol):

function removeElementById(remele) {
remele = document.getElementById(remele);
if (remele.parentNode && remele.parentNode.removeChild(remele)) {
remele.parentNode.removeChild(remele); }
}

removeElementById("storyarea");

I hope this may help someone out there
 
R

RobG

Jam said:
Ok well this is how Im working this script in 'final' (lol):

Please don't post code with tabs, replace them with spaces. It nearly
always
causes wrapping that introduces errors.
function removeElementById(remele) {
remele = document.getElementById(remele);
if (remele.parentNode && remele.parentNode.removeChild(remele)) {

Here you will actually call the removeChild method, not just test it.
The intention is to test the method, and if it's supported, call it:

if ( remele.parentNode && remele.parentNode.removeChild ) {
remele.parentNode.removeChild(remele); }
}


If the method is not supported, you will get an error from the if(..),
if the method is supported, you will get an error from this line
because 'remele' was already removed in the test.
removeElementById("storyarea");

I hope this may help someone out there

I hope this helps you! 8-p
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top