How to clear/remove all the objects in a DIV?

305 views
Skip to first unread message

justaguy

unread,
Jun 19, 2013, 2:14:09 PM6/19/13
to
Hi,

I have this DIV below,

<DIV id="workspace"> ... lots of dynamically generated objects here... </DIV>

Now, i want this div to be empty, and attempted with document.getElementById('workspace').textContent = '' with no effects.

What's the correct syntax?

Thanks.

Thomas 'PointedEars' Lahn

unread,
Jun 19, 2013, 2:36:34 PM6/19/13
to
justaguy wrote:

> I have this DIV below,
>
> <DIV id="workspace"> ... lots of dynamically generated objects here...
> </DIV>
>
> Now, i want this div to be empty, and attempted with
^
> document.getElementById('workspace').textContent = '' with no effects.
>
> What's the correct syntax?

This syntax is essentially correct (again you should have posted the
*relevant* parts of the *real* code in a *separate* paragraph); the whole
markup or browser (DOM implementation) may not. “With no effects” is most
certainly a wrong description, see <http://devtoolsecrets.com/>.

The personal pronoun for oneself is *always* spelled with a capital letter
in English.

Get a real name. Do not use Google Groups for posting if you cannot work
around its bugs (e.g., producing lines of astronomical length).

--
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

justaguy

unread,
Jun 19, 2013, 4:57:26 PM6/19/13
to
I now have a better syntax,
var node = document.getElementById('workspace');
while (node.hasChildNodes()) {node.removeChild();};

But still, it does not remove all the newly inserted objects inside the 'workspace' DIV, how come?

Thanks.

JJ

unread,
Jun 19, 2013, 6:20:33 PM6/19/13
to
On Wed, 19 Jun 2013 13:57:26 -0700 (PDT), justaguy wrote:
> I now have a better syntax,
> var node = document.getElementById('workspace');
> while (node.hasChildNodes()) {node.removeChild();};
>
> But still, it does not remove all the newly inserted objects inside the 'workspace' DIV, how come?

Do you even know what that code does exactly?

Gregor Kofler

unread,
Jun 19, 2013, 6:57:44 PM6/19/13
to
Am 19.06.2013 22:57, justaguy meinte:
Maybe because element.removeChild() requires the node to be removed as
an argument?

Gregor

Thomas 'PointedEars' Lahn

unread,
Jun 19, 2013, 7:16:33 PM6/19/13
to
Gregor Kofler wrote:

> Am 19.06.2013 22:57, justaguy meinte:
>> I now have a better syntax,
>> var node = document.getElementById('workspace');
>> while (node.hasChildNodes()) {node.removeChild();};
>>
>> But still, it does not remove all the newly inserted objects inside the
>> 'workspace' DIV, how come?
>
> Maybe because element.removeChild() requires the node to be removed as
> an argument?

And therefore throws “Error: NotFoundError: DOM Exception 8” and the like to
the script console…

justaguy

unread,
Jun 19, 2013, 8:21:17 PM6/19/13
to
I saw two samples of supposedly removing all the objects within an element.

a)
var node = document.getElementById('workspace');
while (node.hasChildNodes()) {
node.removeChild(node.lastChild);
}

b)
var node = document.getElementById('workspace');
while (node.hasChildNodes()) {
node.removeChild(node.firstChild);
}

I tried b), all the objects are still inside the 'workspace' DIV.
And I don't why it's called 'firstChild', thought it should be every 'child' within... so, did something for the heck of it of
while (node.hasChildNodes()) {
node.removeChild()
}

Weird tho, with Firefox 21 for Windows, no error returned.



justaguy

unread,
Jun 19, 2013, 8:39:37 PM6/19/13
to
I just took another look, there appeared to be a typo of the id of 'workspace', very weird, I usually copied and pasted it...
Anyway, corrected the typo,
node.removeChild(node.lastChild) etc. works.

HOWEVER, I now have a new requirement, that is, I wonder how we could remove every object between two IDs within a DIV, and let me continue to use this DIV of id 'workspace'.

It looks like this:
<DIV id="workspace">
<table>
<tr><td>abc</td> <td>...</td> </tr>
...
<tr id="start"><td>abc</td> <td>...</td> </tr>
...
<tr id="stop"><td>abc</td> <td>...</td> </tr>
...
<tr><td>abc</td> <td>...</td> </tr>
</table>
</DIV>

And there are a bunch of objects dynamically inserted into this DIV and between
the ID of 'start' and 'stop',
the new question is, how do we remove every object between
'start' and 'stop'?
Is there something like removeBefore(nodeName or ID) or deleteBefore (nodeName or ID) or removeAfter or deleteAfter?

Many thanks.

justaguy

unread,
Jun 20, 2013, 10:05:27 AM6/20/13
to
On Wednesday, June 19, 2013 2:14:09 PM UTC-4, justaguy wrote:
Update: I've solved the problem. Tho the syntax of removing object by its position (removeBefore or removeAfter) would be interesting as well.


Denis McMahon

unread,
Jun 20, 2013, 11:27:12 AM6/20/13
to
On Wed, 19 Jun 2013 17:39:37 -0700, justaguy wrote:

> Is there something like removeBefore(nodeName or ID) or deleteBefore
> (nodeName or ID) or removeAfter or deleteAfter?

Is it possible that when you delete the immediate children, their
descendants are still present?

Your div is the root of a tree. What you need to do is trim the tree back
to the root starting at the most distant branch.

Possibly something like this:

function remove_children_and_node( node ) {
while ( node.hasChildNodes() )
remove_children_and_node( node.firstChild );
node.parentNode.removeChild( node );
}

x = document.getElementById("myDiv");
while ( x.hasChildNodes() )
remove_children_and_node( x.firstChild );

Not tested, obviously.

--
Denis McMahon, denismf...@gmail.com

Thomas 'PointedEars' Lahn

unread,
Jun 20, 2013, 12:15:31 PM6/20/13
to
Denis McMahon wrote:

> On Wed, 19 Jun 2013 17:39:37 -0700, justaguy wrote:
>> Is there something like removeBefore(nodeName or ID) or deleteBefore
>> (nodeName or ID) or removeAfter or deleteAfter?
>
> Is it possible that when you delete the immediate children, their
> descendants are still present?

No. Deleting a node deletes all of its descendants. However, care must be
taken regarding the event listeners on descendents as some implementations
do not clean up properly.

> Your div is the root of a tree. What you need to do is trim the tree back
> to the root starting at the most distant branch.

No, you do not. You just use the “previousSibling” property repeatedly.

> […]
> Not tested, obviously.

Like your other junk postings.

Dr J R Stockton

unread,
Jun 20, 2013, 2:52:38 PM6/20/13
to
In comp.lang.javascript message <4145994.A...@PointedEars.de>,
Wed, 19 Jun 2013 20:36:34, Thomas 'PointedEars' Lahn
<Point...@web.de> posted:

>The personal pronoun for oneself is *always* spelled with a capital letter
>in English.

That is certainly false, as is shown by what you quote, and by the fact
that in English speech it is never spelt at all. Think before you
write, and continue doing so until you achieve correctness and
exactitude. And, of course, read e e cummings, and "archy and
mehitabel".

--
(c) John Stockton, Surrey, UK. �@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

Christoph Michael Becker

unread,
Jun 20, 2013, 7:25:11 PM6/20/13
to
Dr J R Stockton wrote:
> Think before you write, and continue doing so until you achieve
> correctness and exactitude.

Good advice. But if we all adhere to this, nobody would post in this
(or any other) newsgroup, methinks.

> [...] by the fact that in English speech it is never spelt at all.

AFAIK nothing is ever *spelled* in any *spoken* language at all. But
then, Usenet is not about *speaking*, but about *writing*.

BTW: Have you even considered changing the subject of your post, as it
is not related to the topic at all?

--
Christoph M. Becker

Joao Rodrigues

unread,
Jun 20, 2013, 8:01:44 PM6/20/13
to
On 20/06/2013 13:15, Thomas 'PointedEars' Lahn wrote:
> Denis McMahon wrote:
>> Your div is the root of a tree. What you need to do is trim the tree back
>> to the root starting at the most distant branch.
>
> No, you do not. You just use the “previousSibling” property repeatedly.
>
>> […]
>> Not tested, obviously.
>
> Like your other junk postings.
>

Stalker troll.

Evertjan.

unread,
Jun 21, 2013, 3:41:41 AM6/21/13
to
Christoph Michael Becker wrote on 21 jun 2013 in comp.lang.javascript:

> Dr J R Stockton wrote:

>> [...] by the fact that in English speech it is never spelt at all.
>
> AFAIK nothing is ever *spelled* in any *spoken* language at all.

Your "AFAIK" and "any" are mutual exclusive.

Perhaps you are under the delusion of a permanent monoglottic or
parvoglottic spell?

> But then, Usenet is not about *speaking*, but about *writing*.

If you "say" so.

=========== btw =================

Spelled vs. spelt

In American English, spelt primarily refers to the hardy wheat grown
mostly in Europe, and the verb spell makes spelled in the past tense and
as a past participle. In all other main varieties of English, spelt and
spelled both work as the past tense and past participle of spell, at least
where spell means to form words letter by letter or (with out) to make
clear. Outside the U.S., the two forms are interchangeable in these uses,
and both are common.

But when spell carries the sense to temporarily relieve (someone) from
work, spelled is the preferred form throughout the English-speaking world.
This is a minor point, though, as this sense of spell is rarely used
outside the U.S., where it is most common.

Spelled is not a recent Americanism, as many people assume (including some
who have commented on this post). Both spelled and spelt are old, and
examples of each are easily found in historical Google Books searches
covering the 17th and 18th centuries. It is true, however, that spelt was
ascendant everywhere through most of the 19th century. This ended when
Americans permanently settled on spelled around 1900.

<http://grammarist.com/spelling/spelled-spelt/>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Gene Wirchenko

unread,
Jun 21, 2013, 3:52:00 PM6/21/13
to
On Fri, 21 Jun 2013 01:25:11 +0200, Christoph Michael Becker
<cmbec...@arcor.de> wrote:

>Dr J R Stockton wrote:
>> Think before you write, and continue doing so until you achieve
>> correctness and exactitude.
>
>Good advice. But if we all adhere to this, nobody would post in this
>(or any other) newsgroup, methinks.
>
>> [...] by the fact that in English speech it is never spelt at all.
>
>AFAIK nothing is ever *spelled* in any *spoken* language at all. But
>then, Usenet is not about *speaking*, but about *writing*.

What is a spelling bee then?

>BTW: Have you even considered changing the subject of your post, as it
>is not related to the topic at all?

Sincerely,

Gene Wirchenko

John Harris

unread,
Jun 24, 2013, 4:03:34 PM6/24/13
to
On Fri, 21 Jun 2013 12:52:00 -0700, Gene Wirchenko <ge...@telus.net>
wrote:

<snip>
> What is a spelling bee then?
<snip>

It's people speaking about writing of course :-)

John

Evertjan.

unread,
Jun 25, 2013, 4:50:22 AM6/25/13
to
Thought it was about a godspell of bees spilling honey.

Christoph Michael Becker

unread,
Jun 28, 2013, 5:56:10 PM6/28/13
to
Evertjan. wrote:
> Christoph Michael Becker wrote on 21 jun 2013 in comp.lang.javascript:
>
>> Dr J R Stockton wrote:
>
>>> [...] by the fact that in English speech it is never spelt at all.
>>
>> AFAIK nothing is ever *spelled* in any *spoken* language at all.
>
> Your "AFAIK" and "any" are mutual exclusive.

IBTD.

> Perhaps you are under the delusion of a permanent monoglottic or
> parvoglottic spell?

I hope that I am not under any spell. BTW: what's the meaning of
"parvoglottic"? Looking it up with Google just brings me back here. :)

>> But then, Usenet is not about *speaking*, but about *writing*.
>
> If you "say" so.

Well "said". :)

--
Christoph M. Becker

Evertjan.

unread,
Jun 28, 2013, 6:19:09 PM6/28/13
to
Christoph Michael Becker wrote on 28 jun 2013 in comp.lang.javascript:

>> Perhaps you are under the delusion of a permanent monoglottic or
>> parvoglottic spell?
>
> I hope that I am not under any spell. BTW: what's the meaning of
> "parvoglottic"?

Not understanding this word is a typical example of the same.

Lat. parvus. -a, -um = small, little, cheap

glot you should understand, if you understood monoglottic.

> Looking it up with Google just brings me back here. :)

Ah, quelle perfection de recursion!

<http://aiellodba.blogspot.nl/2011/09/la-recursion-es-divina.html>

Jukka K. Korpela

unread,
Jun 28, 2013, 6:20:24 PM6/28/13
to
2013-06-29 0:56, Christoph Michael Becker wrote:

> IBTD.

Consider the opportunity of looking much less foolish by posting
sensible on-topic messages instead of ETLAs that were not funny ever.

Then again, due to constant trolling and other disruptions, this group
seems to be close to being virtually dead. Are you sure you want to be
part of making this final this way?

--
Yucca, http://www.cs.tut.fi/~jkorpela/
Reply all
Reply to author
Forward
0 new messages