JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 8th, 2008, 08:56 AM
devinia devinia is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 40 devinia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 7 m 32 sec
Reputation Power: 3
Get all id's in current document

Hi,
slowly getting my head around DOM syntax and reading online tut's and stuff but stuck with correct syntax to get all the id's in a document.

Syntax i presume is similar to the, getElementsByTagName - but i need to do this for 'id' attributes (i need each id's value i.e. the name assigned to it, not just an indication as to whether the element has an id attribute).

Can i use syntax like:
Code:
var id_arry=document.getElementById(*)


and return an array of the id's or maybe use this syntax in a for loop which returns each element on the page with an id attribute so i can add it to an array manually etc ...

Can it also be done with the 'class' attribute???

with thanx in advanz ...

Last edited by devinia : May 8th, 2008 at 08:57 AM. Reason: more clarity required in title

Reply With Quote
  #2  
Old May 8th, 2008, 09:21 AM
KorRedDevil's Avatar
KorRedDevil KorRedDevil is online now
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Aug 2005
Location: Bucharest ROMANIA
Posts: 1,506 KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 14 h 40 sec
Reputation Power: 55
Send a message via Yahoo to KorRedDevil
No.

getElementById() supposes to return always a single element, while getElementsByName() and getElementsByTagName() return always a collection (array). Methods are different, thus getElementById() can not use a wild card such as "*".

About the classes. No, DOM has no native method to refer elements by their class.

In both cases (as for any other native HTML attribute or custom JSON properties of the HTML elements) a workaround is available, on using getElementsByTagName('*') as start:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function returnAttributes(at){
var arr=[];
var elem=document.getElementsByTagName('*'), i=0, e;
while(e=elem[i++]){
e[at]?arr[arr.length]=e[at]:null;
}
return arr;
}
onload=function(){
var allIds=returnAttributes('id');
var allClasses=returnAttributes('className');
alert(allIds);
alert(allClasses)
}
</script>
</head>
<body>
<div id="id_1"></div>
<div class="myclass_1"></div>
<div></div>
<div id="id_2"></div>
<div class="myclass_2"></div>
</body>
</html>
__________________
HELP SAVE ANA

Reply With Quote
  #3  
Old May 8th, 2008, 09:46 AM
devinia devinia is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 40 devinia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 7 m 32 sec
Reputation Power: 3
Quote:
Originally Posted by KorRedDevil
No.

getElementById() supposes to return always a single element, while getElementsByName() and getElementsByTagName() return always a collection (array). Methods are different, thus getElementById() can not use a wild card such as "*".

About the classes. No, DOM has no native method to refer elements by their class.

In both cases (as for any other native HTML attribute or custom JSON properties of the HTML elements) a workaround is available, on using getElementsByTagName('*') as start:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function returnAttributes(at){
var arr=[];
var elem=document.getElementsByTagName('*'), i=0, e;
while(e=elem[i++]){
e[at]?arr[arr.length]=e[at]:null;
}
return arr;
}
onload=function(){
var allIds=returnAttributes('id');
var allClasses=returnAttributes('className');
alert(allIds);
alert(allClasses)
}
</script>
</head>
<body>
<div id="id_1"></div>
<div class="myclass_1"></div>
<div></div>
<div id="id_2"></div>
<div class="myclass_2"></div>
</body>
</html>



Thanks KorRedDevil,
One point, am i right in thinking,
Code:
e[at]
is a regular expression string comparison?? don't want to just copy it without fully understanding each line - i've got a handle on everything else you kindly shown though - i'll try this and much thanks for your kind help.

Reply With Quote
  #4  
Old May 8th, 2008, 10:14 AM
KorRedDevil's Avatar
KorRedDevil KorRedDevil is online now
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Aug 2005
Location: Bucharest ROMANIA
Posts: 1,506 KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 14 h 40 sec
Reputation Power: 55
Send a message via Yahoo to KorRedDevil
it is square bracket notation. An attribute as
Code:
element.id

can be also written as:
Code:
element['id']

It is useful when handling dynamically the attributes/properties.
Code:
function returnAttributes(at){
...
e[at]
//means e.id if the argument passed is 'id', 
//or e.className if the argument passed is 'className'
...
}


The expression:
Code:
condition?statement if true:statement if false;

is a ternary operator, the condensed equivalent of:
Code:
if(condition)
{statement if true}
else
{statement if false}

In my code that ternary checks first for the existence of that attribute, and only if the element has that attribute, its value is input into the array.

e[at] // if the element has that type of attribute
?arr[arr.length]=e[at] //the next array's element is the attribute's value
:null;//else do nothing

Last edited by KorRedDevil : May 8th, 2008 at 10:19 AM.

Reply With Quote
  #5  
Old May 8th, 2008, 10:28 AM
devinia devinia is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 40 devinia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 7 m 32 sec
Reputation Power: 3
Nice,
Really like that code, fairly experienced in PHP but never used that kind of square bracket notation in that way - its like, Object.method or object.property. At that point with my meagre JS knowledge i would have used 'indexOf' to match the arg with 'id' (or classname) string. Also noticed when i run the code it produces a comma sepertated list, ???? how the, where the, what the ... i don't consider myself a Javascript/DOM officionado but whoa. Anyway its works just great much thanks again.

Reply With Quote
  #6  
Old May 8th, 2008, 01:09 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Click here for more information.
 
Join Date: Jul 2004
Location: USA
Posts: 15,149 Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Weeks 2 Days 2 h 47 m 18 sec
Reputation Power: 1294
When you call alert() on the arrays, the toString() method is automatically called on them. The toString() method of arrays works the same as join(',').
Quote:
Originally Posted by Kor
About the classes. No, DOM has no native method to refer elements by their class.

Firefox 3, Safari 3.1, and Opera 9.5 (will) have native implementations of document.getElementsByClassName().
http://developer.mozilla.org/en/doc...entsByClassName
http://webkit.org/blog/153/webkit-g...ntsbyclassname/
http://virtuelvis.com/archives/2007/09/kestrel

It's part of the HTML5 working drafts, since most people consider it to be a missing feature of DOM2-HTML.
__________________
Spreading knowledge, one newbie at a time.

Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions

IE7: the generation 7 browser new in a world of generation 8 browsers.
Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

Reply With Quote
  #7  
Old May 8th, 2008, 01:53 PM
KorRedDevil's Avatar
KorRedDevil KorRedDevil is online now
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Aug 2005
Location: Bucharest ROMANIA
Posts: 1,506 KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level)KorRedDevil User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 14 h 40 sec
Reputation Power: 55
Send a message via Yahoo to KorRedDevil
Quote:
Originally Posted by Kravvitz
Firefox 3, Safari 3.1, and Opera 9.5 (will) have native implementations of document.getElementsByClassName().
http://developer.mozilla.org/en/doc...entsByClassName
http://webkit.org/blog/153/webkit-g...ntsbyclassname/
http://virtuelvis.com/archives/2007/09/kestrel

It's part of the HTML5 working drafts, since most people consider it to be a missing feature of DOM2-HTML.

Right. I didn't say it will have not. As long as, unfortunately, IE does not follow, we will still use workarounds

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Get all id's in current document


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway