473,387 Members | 1,844 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,387 software developers and data experts.

Is there getAllElementIds() function?

RC
The Subject says its all.

document.getElementById('id');

only return an object with id name.

I am looking for a function
return array of objects.

document.getAllElementIds();

BTW, I am also looking for

document.getAllElementNames();
document.getElementByName("name");

function?
where "name" is from HTML

<tag name="document_name" ....>
similar to
<tag id="id_name" ....>

document.anchors return array of <a>anchor</a>
document.forms return array of <form>
document.images return array of <img>
document.links return array of <a>
But why there is no
document.ids return array of ids?
May 16 '06 #1
8 1261
RC wrote:
I am looking for a function
return array of objects.
document.getAllElementIds();


Why?

When you're using ID's, which must be unique, it's assumed that you know
what is on the page and what its ID is. Otherwise, why would it have an ID?

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
May 16 '06 #2
RC wrote:
The Subject says its all.

document.getElementById('id');

only return an object with id name.

I am looking for a function
return array of objects.
There isn't one. You could use:

var allEls = document.getElementsByTagName('*');

then sift through the returned collection to find all elements using
(untested):

var el, idEls=[];
var i = allEls.length;
while (i--) {
el = allEls[i];
if ( el.id && el.id != '') idEls.push(el);
}

But I expect that will be very slow and ridiculously processor
intensive. Explain more about what you are trying to do and maybe
someone will have a better way to achieve it.

document.getAllElementIds();

BTW, I am also looking for

document.getAllElementNames();
You could use something like that above for IDs, however elements less
than half the elements in HTML 4 can have a name attribute.

document.getElementByName("name");
There is getElementsByName() already, it returns a collection. If you
know only one element has a particular name, or you want the first one
with that name, then:

var namedEl = document.getElementsByName('someName')[0];
should do the trick.

function?
where "name" is from HTML

<tag name="document_name" ....>
similar to
<tag id="id_name" ....>

document.anchors return array of <a>anchor</a>
document.forms return array of <form>
document.images return array of <img>
document.links return array of <a>
But why there is no
document.ids return array of ids?


Explain why you want that. All of the above are collections of one
particular type of element, document.ids (if it existed) could be a
mixed collection of nearly any type of element.
--
Rob
May 16 '06 #3
RC
RobG wrote:
Explain more about what you are trying to do and maybe
someone will have a better way to achieve it.


First, thank Q very much for the long reply.

If my HTML file have 20 or more id names like

<div id="id1">
....
<div id="idN">

I really don't want to do

var arrayOfId = new Array(20);

arrayOfId[0] = document.getElementById('id1');
....
arrayOfId[19] = document.getElementById('idN');

will be nice if I can

var arrayOfId[] = document.getAllElementIds();

In my case, I am only interest <div id="...">

If I name all div in the same name like

<div name="mydiv" id="id1">
....
<div name="mydiv" id="idN">

Then can I

var mydivname = document.getElementsByName("mydiv");
var arrayIds = new Array(mydivname.id.lenght);
for (var i = 0; i < arrayIds.length; i++)
arrayIds[i] = document.getElementById(mydivname.id[i].value);
May 16 '06 #4
Rob
In your code:
var mydivname = document.getElementsByName("mydiv");
var arrayIds = new Array(mydivname.id.lenght);
This does not create an array of .length
for (var i = 0; i < arrayIds.length; i++) //So this .lenght is always
one
arrayIds[i] = document.getElementById(mydivname.id[i].value);// this
is wrong in several ways.

I rewrote and tested your code below.
<div name="mydiv" id="id1">ID1</div>
<div name="mydiv" id="id2">ID2</div>
<script>
var mydivname = document.getElementsByName("mydiv");
alert("Found "+mydivname.length+" elements with name mydiv")
var arrayIds = new Array();
for (var i = 0; i < mydivname.length; i++) {
arrayIds.push(document.getElementById(mydivname[i].id));
alert("arrayIds["+i+"] is "+arrayIds[i].id);
}
</script>
So I'm still not clear on what you are trying to accomplish. Now you
have an array which contains all your DIV ids. I'm not sure I see an
applicaton for such an array (but I'm new at this so I may be missing
something:).

Rob:-]

May 16 '06 #5
RC
Rob wrote:
So I'm still not clear on what you are trying to accomplish. Now you
have an array which contains all your DIV ids. I'm not sure I see an
applicaton for such an array (but I'm new at this so I may be missing
something:).


Hmhmhmhmhm........hahaha...!!!

I am try to make my own pull down menu in DHTML = JavaScript + CSS
I know there are many JavaScript Menu out there to download.
But they all require you have greate amount of JavaScript programming
skills. I hate to read somebody's poor documents.

So I designed write by my own. Just HTML with
minimum JavaScript for onMouseOver, onMouseOut and onClick
plus a CSS file.

<a href=# onMouseOver="pulldown('mydiv0')">Menu 0</a>

<a href=# onMouseOver="pulldown('mydiv1')">Menu 1</a>
.....

<a href=# onMouseOver="pulldown('mydivN')">Menu N</a>

You really can't do onMouseOut="collapse('mydivid')"
Because let's say you move mouse pointer to Menu 1,
it do pulldown for mydiv1, greate! but once you
move the pointer to the sub-menu in Menu 1, it then do
collapse for Menu 1. so I NEVER able to select sub-menu from
Menu 1.

Therefore, when I move mouse pointer to Menu 1, I want to make sure
Menu 0, Menu 2, Menu N all are collapse if the are/were pull down.
Similar, when I move mouse pointer to Menu 0, Menu 1, Menu 2, ..
will collapse.

Now you understand why I want get an array of <div name="mydiv" id="xx">
?

Hahahaha........!!!

Again, thank Q very much to the test for me. I'll use your part of codes
to finish my last part of programs
May 16 '06 #6
Rob
So you might try this code in your anchor tags:

<div name="mydiv" id="id1"><a href="#"
onMouseOver="pulldown(this.parentNode.id);">ID1</div>
<div name="mydiv" id="id2"><a href="#"
onMouseOver="pulldown(this.parentNode.id);">ID2</div>
<script>
function pulldown(divId){
alert("onMouseOver("+divId+")");
}
var mydivname = document.getElementsByName("mydiv");
//alert("Found "+mydivname.length+" elements with name mydiv")
var arrayIds = new Array();
for (var i = 0; i < mydivname.length; i++) {
arrayIds.push(document.getElementById(mydivname[i].id));
// alert("arrayIds["+i+"] is "+arrayIds[i].id);
}
</script>

This way the only thing you have to change on each menu item is the DIV
id. (Well, I guess you'll be filling in actual href URLs too:)

I tested it and it works.

Rob:-]

May 16 '06 #7
RC wrote:
RobG wrote:
Explain more about what you are trying to do and maybe someone will
have a better way to achieve it.
First, thank Q very much for the long reply.

If my HTML file have 20 or more id names like

<div id="id1">
...
<div id="idN">

I really don't want to do

var arrayOfId = new Array(20);

arrayOfId[0] = document.getElementById('id1');
...
arrayOfId[19] = document.getElementById('idN');


You could use a loop, and if there are only 20 divs it would be
reasonably fast:

var arrayOfId = [];
for (var i=0; i<20; ++i) {
arrayOfId[i] = document.getElementById('id' + i)
}

or if the divs are numbered consecutively and contiguously:

var arrayOfId = [];
var i = 0;
var div;
while ( (div=document.getElementById('id' + i++)) ){
arrayOfId[i] = div;
}

with appropriate feature testing.

will be nice if I can

var arrayOfId[] = document.getAllElementIds();

In my case, I am only interest <div id="...">

If I name all div in the same name like

<div name="mydiv" id="id1">
...
<div name="mydiv" id="idN">
'name' is not a valid attribute for a div element in HTML 4.

<URL:http://www.w3.org/TR/html4/struct/global.html#edef-DIV>

Using names and getElementsByName with divs does work in some browsers,
but you can't expect it to work everywhere.

Then can I

var mydivname = document.getElementsByName("mydiv");
var arrayIds = new Array(mydivname.id.lenght);
for (var i = 0; i < arrayIds.length; i++)
arrayIds[i] = document.getElementById(mydivname.id[i].value);


getElementsByName() returns a collection, which has some of the
properties of an array. What array properties do you want to use that a
collection doesn't have? There are many simple drop-down menus that use
little or no script, most are based on UL/LI elements.

Google is your friend, there are a number of them on
<URL:http://www.alistapart.com/>.
--
Rob
May 16 '06 #8
RC wrote:
<snip>
Now you understand why I want get an array of <div
name="mydiv" id="xx"> ?

<snip>

The HTML DTDs do not define a NAME attribute for DIV elements.
Consequently not all browsers will return DIV elements with NAME
attributes through the - document.getElementsByName - method. As an
attempt to produce cross-browser results your strategy is already dead
in the water.

Richard.
May 16 '06 #9

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.