473,320 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Compatibility

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
Jul 23 '05 #1
11 1254
Ken wrote:
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').parentNod e.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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #2
Ken wrote:
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
Jul 23 '05 #3
RobG wrote:
[snip]
function deleteIt(x) {
x.parentNode.remvoeChild.x;


Oooops, that should be:

x.parentNode.removeChild(x);

Dang rented fingers...

Rob.
Jul 23 '05 #4
RobG wrote:
Ken wrote:
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.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #5
Randy Webb wrote:
[snip]
Did you test that?


Obviously not, or I'd not have been so wrong. My bad, and apologies.

:-( Rob.
Jul 23 '05 #6
Ken
"Randy Webb" <Hi************@aol.com> wrote in message
news:Nv********************@comcast.com...
Ken wrote:
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').parentNod e.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
comp.lang.javascript FAQ - http://jibbering.com/faq


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.removeC hild('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').appendChi ld(image_display);
alert("test");
document.getElementById('image_display').parentNod e.removeChild('image_displ
ay');
<div id='image_display' ></div>
Image was created but not removed

Ken
Jul 23 '05 #7
On Wed, 27 Oct 2004 07:38:35 GMT, Ken <kk******@wi.rr.com> wrote:

[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.removeC hild(
'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 Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #8
On Tue, 26 Oct 2004 23:03:16 -0400, Randy Webb <Hi************@aol.com>
wrote:

[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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #9
Randy Webb <Hi************@aol.com> writes:
document.getElementById('image_display').parentNod e.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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #10
Lasse Reichstein Nielsen wrote:
Randy Webb <Hi************@aol.com> writes:

document.getElementById('image_display').parentN ode.removeChild('image_display');
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #11
Ken
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opsgiuukkpx13kvk@atlantis...
On Wed, 27 Oct 2004 07:38:35 GMT, Ken <kk******@wi.rr.com> wrote:

[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.removeC hild(
'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 Winter
Replace ".invalid" with ".uk" to reply by e-mail.


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
Jul 23 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jez Naisbitt | last post by:
Hi Guys, After a break of 2 years I'm now re-visiting the world of java. I recall on my last foray that I had to stick to java 1.1 so I could deploy applets from a server and obtain maximum...
0
by: flat_ross | last post by:
If there was one thing nice about "Binary Compatibility" in VB6, it would tell you at compile time that you changed your public interface. I am looking for the same functionality in .NET. I know...
6
by: someone | last post by:
Suppose that I have a class in an assembly that is delivered to the user, what can I do to change the class so that it doesn't break the binary compatibility? That is, user application can run...
13
by: Derek | last post by:
As I understand it there is a good amount of link compatibility among C compilers. For example, I can compile main.c with GCC and func.c with Sun One and link the objects using either linker (GNU...
2
by: Dominic | last post by:
Hi everybody, I'm planning to use serialization to persist an object (and possibly its child objects) in my application. However, I'm concerned about the backward compatibility issue. I'm...
1
by: Vycka | last post by:
Hello, There is a enterprise web application that is based on asp.net technologies and works on Microsoft IIS. The total number of users is 850. When the load of system gets very high, the...
14
by: frostalicious | last post by:
Used VB.NET (on my client PC) to convert VB6 executable to .NET executable. Placed the .exe file on a network drive on my server. From client, ran .NET Wizards "Trust an Assembly" to make the...
2
by: Carlo | last post by:
I recently started in a new position, and I inherited an application written in VB6 that uses a bunch of DLLs and OCX controls. Version Compatibility is set to Binary at the project level, but since...
1
by: Simon Woods | last post by:
Hi I have a dll ('dll-X') which runs on top of (dependent upon) several other dlls. My build environment has a folder structure binaries compat-libs
17
by: osama178 | last post by:
Hi, What does it mean for an object to be binary compatible? And why aren't STL objects binary compatible? Any insights, links, resources for further reading are greatly appreciated. Thanks.
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.