|
-
Jul 16th, 2006, 08:58 AM
#1
[RESOLVED] Check if element exists
I'm currently creating an element on a page dynamically to show image. I want to prevent more than one image being created at a time - so i figured I'd check for it's presence. A little scan of MSDN has given me getElementByName - but I don't know what methods / properties the returned collections has. I had a stab at .count (but no joy):
Code:
var iexists = document.getElementsByName('popupimg')
alert(iexists.count)
var i = document.createElement('img');
i.name = 'popupimg'
If someone could advise me on the best way of doing this and/or point me in the way of the relevent documentation, that would be great.
-
Jul 16th, 2006, 09:13 AM
#2
Re: Check if element exists
Ok, I've worked out you can do:
Code:
alert(iexists.length)
Works in Fx, but not in IE! Presumably getElementsByName isn't returning anything
-
Jul 16th, 2006, 09:14 AM
#3
Re: Check if element exists
Code:
i.setAttribute('id', 'popupimg');
...
var iexists = document.getElementById('popupimg');
if (iexists) alert('It is there');
Since there is only one, there is no need to get a collection of elements. Name is deprecated, you should use ID.
-
Jul 16th, 2006, 09:22 AM
#4
Re: Check if element exists
woo, thanks for the quick reply Merri - works great 
Edit: I have to spread it around a bit I'm afraid.
-
Jul 16th, 2006, 09:53 AM
#5
Re: [RESOLVED] Check if element exists
The problem is that IE doesn't implement getElementsByName. And be warned: it incorrectly implements getElementById, as it will also return elements with their name attribute set to the passed value, and will return an array on multiple hits. (The latter wouldn't be incorrect if it didn't have the first bug, as a document with a non-unique ID is not well-formed and all DOM operations thus undefined, but since IE also collects elements by their name attribute, well-formed documents can have an array as the return value of getElementById.)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 16th, 2006, 10:31 AM
#6
Re: [RESOLVED] Check if element exists
ok, thanks for the info - shouldn't be a problem here.
I have another question though!
how would i go about removing the iexists object from the page. i have this code for when the image gets clicked:
Code:
function doPopupClick(e)
{
var obj = e.target || e.srcElement;
obj.parentNode.removeChild(obj);
}
but I'm unsure as to how to modify that (if indeed i should be doing it that way). I don't know what e is, is it the object that's been clicked?
-
Jul 16th, 2006, 10:43 AM
#7
Re: [RESOLVED] Check if element exists
Try this.parentNode.removeChild(this);
this can be replaced with the result object of a getElementById.
Edit!
You also might find this article interesting.
-
Jul 16th, 2006, 10:47 AM
#8
Re: [RESOLVED] Check if element exists
woo iexists.parentNode.removeChild(iexists); works - i was trying that as you posted, but it wasn't working because I still had the alert in - why would that stop it functioning?
-
Jul 16th, 2006, 10:49 AM
#9
Re: [RESOLVED] Check if element exists
What was the code you had?
-
Jul 16th, 2006, 10:53 AM
#10
Re: [RESOLVED] Check if element exists
Code:
function PopupPic(sPicURL)
{
var iexists = document.getElementById('popupimg');
if (iexists)
//alert('It is there');
iexists.parentNode.removeChild(iexists);
else
var i = document.createElement('img');
i.setAttribute('id', 'popupimg')
i.setAttribute('src', sPicURL);
.
.
I uncomment the alert and it stops working.
cheers for that link, I'll have a look - I'm doing work for my g/f's dad - but i don't know any web languages so I'm learning as i go along
-
Jul 16th, 2006, 11:03 AM
#11
Re: [RESOLVED] Check if element exists
You need braces around the conditional blocks:
Code:
if (iexists) {
//alert('It is there');
iexists.parentNode.removeChild(iexists);
} else {
var i = document.createElement('img');
i.setAttribute('id', 'popupimg')
i.setAttribute('src', sPicURL);
}
You should have gotten JS errors if the image didn't yet exist.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 16th, 2006, 11:09 AM
#12
Re: [RESOLVED] Check if element exists
it wasn't throwing any errors - it just wasn't doing anything.
oh well - with the braces it pops up the alert as well, thanks
-
Jul 16th, 2006, 11:27 AM
#13
Re: [RESOLVED] Check if element exists
If you're using Firefox, you can see the error messages at Tools > JavaScript console.
-
Jul 16th, 2006, 11:34 AM
#14
Re: [RESOLVED] Check if element exists
-
Jul 16th, 2006, 11:40 AM
#15
Re: [RESOLVED] Check if element exists
hmm, i took out the braces to have a look at the errors. when i first load the page the Javascript Console reports a syntax error on the 'else' - however when i try to pop up the image there aren't any errors generated - is that to be expected?
-
Jul 16th, 2006, 11:59 AM
#16
Re: [RESOLVED] Check if element exists
To view all an objects methods/properties you can use this undocumented behaviour in IE or Moz
Code:
var x;
for (x in myObj) {
console.log(x);
}
console.log outputs it to Firebug, replace with something else if you like.
If you have syntax errors in your JS it will throw one error per JS file and not run any code subsequently (edit: in that file)
Last edited by penagate; Jul 16th, 2006 at 12:02 PM.
-
Jul 16th, 2006, 12:01 PM
#17
Re: [RESOLVED] Check if element exists
cheers pen, i'll definitely check out FireBug
-
Jul 16th, 2006, 12:48 PM
#18
Re: [RESOLVED] Check if element exists
 Originally Posted by penagate
To view all an objects methods/properties you can use this undocumented behaviour in IE or Moz
That's not undocumented. The for...in syntax is a well-defined part of the ECMAScript specification. However, Moz and IE differ slightly in their interpretation, as IE does not report built-in functions with this iteration, while Moz does.
The console object, on the other hand, is provided by FireBug and not available on any browser except a Gecko with FireBug installed.
bushmobile, syntax errors are reported when the file is initially parsed, so yes, that's when the page is loaded.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 16th, 2006, 12:49 PM
#19
Re: [RESOLVED] Check if element exists
Sorry, that's what I meant, the implementation details are unspecific.
-
Jul 17th, 2006, 01:25 AM
#20
Re: [RESOLVED] Check if element exists
 Originally Posted by CornedBee
The problem is that IE doesn't implement getElementsByName
I just noticed this, I beg to differ, I am using getElementsByName in a working script for IE/Fx
-
Jul 17th, 2006, 04:44 AM
#21
Re: [RESOLVED] Check if element exists
Hmm ... does it work in IE 5.5? I was always under the impression that IE didn't support gEBN, but it might be an arcane memory.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jul 17th, 2006, 04:46 AM
#22
Re: [RESOLVED] Check if element exists
getElementsByTagName is supported since IE5. IE4 didn't support it.
-
Jul 17th, 2006, 04:49 AM
#23
Re: [RESOLVED] Check if element exists
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|