Syntax verification for removing an element

D

Don Li

Hi,

I'm under the impression that the following code would remove an
element, and I've tested it with IE7. I commented out two lines in
between for I read somewhere that claimed they are not meaninful, your
thought? Thanks.

function removeElementBy(el) {
rem = document.getElementById(el);
// if (rem.parentNode && rem.parentNode.removeChild(rem)) {
rem.parentNode.removeChild(rem);
//}
}
 
M

Martin Honnen

Don said:
I'm under the impression that the following code would remove an
element, and I've tested it with IE7. I commented out two lines in
between for I read somewhere that claimed they are not meaninful, your
thought? Thanks.

function removeElementBy(el) {
rem = document.getElementById(el);

Consider to use
var rem = ...;
to ensure rem is local to the removeElementBy function.
// if (rem.parentNode && rem.parentNode.removeChild(rem)) {
rem.parentNode.removeChild(rem);
//}
}

Checking for the parentNode might make sense under certain conditions
but as long as document.getElementById() gives you the element you want
to remove then it will have a parentNode in any case so you don't need
that check. Doing rem.parentNode.removeChild(rem) twice certainly does
not make sense.
 
J

Joost Diepenmaat

Don Li said:
Hi,

I'm under the impression that the following code would remove an
element, and I've tested it with IE7. I commented out two lines in
between for I read somewhere that claimed they are not meaninful, your
thought? Thanks.

function removeElementBy(el) {
rem = document.getElementById(el);
// if (rem.parentNode && rem.parentNode.removeChild(rem)) {
rem.parentNode.removeChild(rem);
//}
}

The commented lines are wrong. The test tries to remove the child and if
that succeeds (or not, I'm not sure *what* removeChild() returns) tries
to remove it again.

Your question is?
 
D

Don Li

Consider to use
    var rem = ...;
to ensure rem is local to the removeElementBy function.

Thank you. Posting before lunch (on empty stomach) isn't good, silly
me.
 
D

Don Li

The commented lines are wrong. The test tries to remove the child and if
that succeeds (or not, I'm not sure *what* removeChild() returns) tries
to remove it again.

Your question is?

Thank you and the above 'empty stomach' line too...
 
D

Don Li

Ok, a related question, after the following function,
function removeElementBy(el) {
        var rem = document.getElementById(el);
        rem.parentNode.removeChild(rem);    
}

then, I would cursor to stay around that position (the bottom of the
newly removed element position, and we don't know if a given document
has 5 or 15 elements...
how should we go about that?
a) find the requested element id's index?
b) after the "removal" function, set next {ID}.focus()? what if this
is the requested removal ID is last ID?
On right track?

Many thanks.
 
J

Joost Diepenmaat

Don Li said:
then, I would cursor to stay around that position (the bottom of the
newly removed element position, and we don't know if a given document
has 5 or 15 elements...
how should we go about that?
a) find the requested element id's index?

Why do you assume it has an id?
b) after the "removal" function, set next {ID}.focus()?

How about

var rem = document.getElementById(el);
var next = rem.nextSibling;
rem.parentNode.removeChild(rem);
next.focus();

Assuming the element in question is there and can actually handle
focus. Are you using designMode by any change?

Also, your use of "el" as the name of variable containing an id
confusing to me. I prefer to use "el" or "element" only when it actually
is a reference to a DOM element. I use "id" or something similar when it
refers to an id.
what if this
is the requested removal ID is last ID?

Then you need to think about what you mean exactly by "the bottom of the
newly removed element position". Again, your use of ID is confusing.
On right track?

Dunno.
 
D

Don Li

Why do you assume it has an id?
I was talking about a page that I wrote (so, sure it has an id for an
element). But good point, I may not always be consistent...
How about

 var rem = document.getElementById(el);
 var next = rem.nextSibling;
 rem.parentNode.removeChild(rem);
 next.focus();
Got "next has no properties" error with FF 2.x while IE7 complained
about "null"...
Assuming the element in question is there and can actually handle
focus. Are you using designMode by any change?

Also, your use of "el" as the name of variable containing an id
confusing to me. I prefer to use "el" or "element" only when it actually
is a reference to a DOM element. I use "id" or something similar when it
refers to an id.
ok, got you.
Then you need to think about what you mean exactly by "the bottom of the
newly removed element position". Again, your use of ID is confusing.
No bigger, if that's the case forget about the focus() function...

Many thanks.
 

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

No members online now.

Forum statistics

Threads
474,144
Messages
2,570,823
Members
47,369
Latest member
FTMZ

Latest Threads

Top