Compatibility

K

Ken

Over the various browsers, is it better to use:
document.getElementById('num1').removeChild(image_display);
or
image_display.parentNode.removeChild(image_display);

to remove an image.

Ken
 
R

Randy Webb

Ken said:
Over the various browsers, is it better to use:
document.getElementById('num1').removeChild(image_display);
or
image_display.parentNode.removeChild(image_display);

The second one uses an IE-only syntax since it assumes that
image_display is a global variable, so that alone makes the first the
best method to use.

But this is probably best:

document.getElementById('image_display').parentNode.removeChild('image_display');

The problem with the first one is that if there is any white space or
anything else in the parent element, then the image won't be the first
child.
 
R

RobG

Ken said:
Over the various browsers, is it better to use:
document.getElementById('num1').removeChild(image_display);
or
image_display.parentNode.removeChild(image_display);

to remove an image.

Ken

Don't use round brackets to address array elements (it is an IE-ism):

document.getElementById['num1'].removeChild(image_display);

Regarding how to reference the thing you want to delete, it depends on
what you are doing. If the thing you want to delete can be found using
a widely supported and simple DOM method (maybe it's a named node in a
form), then pass a direct reference to it:

<form...>
....
<input type="button" value="Delete something"
onclick="deleteIt(this.form.aNode);">
</form>.

and the function can be really simple:

function deleteIt(x) {
x.parentNode.remvoeChild.x;
}

However, sometimes the document tree will not provide a simple solution
or there is no reliable relationship between the element being clicked
and the one that is to be deleted. In this case, you can pass a string
id and use getElementById(). If the image does not have an id, then
you can trawl through the collection given by
getElementsByTagName('img'), but it is somewhat more long winded.

If using either of the getElement(s) methods, you need to do feature
testing to ensure the users' browser supports it. Rather than put the
feature test code into the thing that is running the function (say a
link or button), you need to include it in the delete function -
otherwise you will be replicating feature test code on every element
that has an action.

Having passed the id of some element that you know you can use to
establish a relationship with the element to be deleted, use
getElementById(), then parentNode :

function deleteIt(theThing) {
if (document.getElementById) {
var a = document.getElementById(theThing);
a.parentNode.remvoeChild(a);
} else {
// some alternative if getElementById not supported
}
}

The parentNode is good because it guarantees the relationship. Also,
by removing any hard coded references from the function, you can use it
to delete lots of things, not just images. Note that you could replace
"a" above with document.getElement....theThing); in each instance, but
it seems neater to use a variable.

Hope that helps! Rob
 
R

Randy Webb

RobG said:
Ken said:
Over the various browsers, is it better to use:
document.getElementById('num1').removeChild(image_display);
or
image_display.parentNode.removeChild(image_display);

to remove an image.

Ken


Don't use round brackets to address array elements (it is an IE-ism):

document.getElementById['num1'].removeChild(image_display);

Did you test that?

document.getElementById('whatever') is not an IE-ism. It's the way it is
to be written.

In fact, document.getElementById['myDiv'] does not even work in IE nor
any other browser.
 
K

Ken

Randy Webb said:
The second one uses an IE-only syntax since it assumes that
image_display is a global variable, so that alone makes the first the
best method to use.

But this is probably best:

document.getElementById('image_display').parentNode.removeChild('image_displ
ay');

The problem with the first one is that if there is any white space or
anything else in the parent element, then the image won't be the first
child.

Randy,

I tried
var image_display=document.createElement('img');
image_display.src='file://' + field;
document.getElementById('num1').appendChild(image_display);
alert("test");
document.getElementById('num1').parentNode.removeChild('image_display');
with <div id="num1"></div>
Image was created but not removed

var image_display=document.createElement('img');
image_display.src='file://' + field;
document.getElementById('image_display').appendChild(image_display);
alert("test");
document.getElementById('image_display').parentNode.removeChild('image_displ
ay');
<div id='image_display' ></div>
Image was created but not removed

Ken
 
M

Michael Winter

[snip]
I tried
var image_display=document.createElement('img');
image_display.src='file://' + field;

Images, even dynamically added ones, should have an alt attribute.
document.getElementById('num1').appendChild(image_display);

No problem here.
document.getElementById('num1').parentNode.removeChild(
'image_display');

This won't work for two very obvious reasons.

1) The removeChild method expects an object reference, not a string. This
would have caused an error.
2) Even if removeChild was passed a reference, you should still have found
an error as the image is not a sibling of the DIV, but its child. In other
words, the parentNode property simply shouldn't be in that statement.

document.getElementById('num1').removeChild(image_display);

[snip]

The second example is exactly the same as the first. You're just using
different names.

By the way, I hope you're using feature detection and more defensive
programming than you're displaying here.

Mike
 
M

Michael Winter

[snip]
In fact, document.getElementById['myDiv'] does not even work in IE nor
any other browser.

Oddly enough, that's what Opera has to try to execute in the Downloads
section of nVidia's nZone (<URL:http://www.nzone.com/>) web site.

The product of generally poor scripting, eval use, and browser detection.
I sent them an e-mail, but I doubt anything will come of it.

Mike
 
L

Lasse Reichstein Nielsen

Randy Webb said:
document.getElementById('image_display').parentNode.removeChild('image_display');

You can't remove strings with removeChild :)
What you perhaps meant is:

var image = document.getElementById('image_display');
image.parentNode.removeChild(image);

/L
 
R

Randy Webb

Lasse said:
You can't remove strings with removeChild :)

That will teach me to type off the top of my head :)
What you perhaps meant is:

var image = document.getElementById('image_display');
image.parentNode.removeChild(image);

Yes and thanks.
 
K

Ken

Michael Winter said:
[snip]
I tried
var image_display=document.createElement('img');
image_display.src='file://' + field;

Images, even dynamically added ones, should have an alt attribute.
document.getElementById('num1').appendChild(image_display);

No problem here.
document.getElementById('num1').parentNode.removeChild(
'image_display');

This won't work for two very obvious reasons.

1) The removeChild method expects an object reference, not a string. This
would have caused an error.
2) Even if removeChild was passed a reference, you should still have found
an error as the image is not a sibling of the DIV, but its child. In other
words, the parentNode property simply shouldn't be in that statement.

document.getElementById('num1').removeChild(image_display);

[snip]

The second example is exactly the same as the first. You're just using
different names.

By the way, I hope you're using feature detection and more defensive
programming than you're displaying here.

Mike

Michael,
I am using feature detection and defensive programming.

I try to only paste the script in question. Quite a bit was not pasted.

Thanks for the comments.

Ken
 

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,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top