By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

javascript function get id value

P: n/a
Is there a function in JavaScript to get the list of the values for all
the 'id' attributes in an HTML page?
Aug 1 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Aug 1, 11:11*am, "Edgardo R. Del Rosario" <axd...@psu.eduwrote:
Is there a function in JavaScript to get the list of the values for all
the 'id' attributes in an HTML page?
No.

You will have to sift through the elements on the page and find those
that have an id attribute with an appropriate value, e.g. where the id
attribute value has a length other than zero, something like:

if (element.id && element.id.length 0)

If you wish to disginguish between those that have the attribute but
no value and those that don't have the attribute at all, e.g. between

<span id="" ...>

and

<span ...>
in a consistent, cross-browser way then you have a bigger challenge.
--
Rob
Aug 1 '08 #2

P: n/a
Edgardo R. Del Rosario wrote:
Is there a function in JavaScript to get the list of the values for all
the 'id' attributes in an HTML page?
No, but you may have an XPath API available which allows you to get a result
containing all element objects representing elements that have the `id'
attribute set. (This is considerably faster that traversing the document
tree yourself, but it is also less compatible.) In its simplest form, which
works for HTML documents in Gecko-based UAs:

var elemsWithId = document.evaluate(
"//*[@id or @iD or @Id or @ID]", document.documentElement, null,
XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);

From this result, you can create the list of IDs:

var e, list = [];
while ((e = elemsWithId.iterateNext())
{
list[list.length] = e.id;
}

Or, suppose you are dealing with invalid markup where duplicate IDs may occur:

var e, ids = new Object(), list = [];
while ((e = elemsWithId.iterateNext())
{
ids[e.id] = true;
}

for (var id in list)
{
list[list.length] = id;
}

In any case, you can then sort the list:

list.sort();
HTH

PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Aug 1 '08 #3

P: n/a
Thomas 'PointedEars' Lahn wrote:
[...]
Or, suppose you are dealing with invalid markup where duplicate IDs may occur:

var e, ids = new Object(), list = [];
while ((e = elemsWithId.iterateNext())
{
ids[e.id] = true;
}

for (var id in list)
Must be

for (var id in ids)

(I renamed the variable name afterwards, but not everywhere.)
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Aug 1 '08 #4

P: n/a
On Jul 31, 9:27 pm, RobG <rg...@iinet.net.auwrote:
On Aug 1, 11:11 am, "Edgardo R. Del Rosario" <axd...@psu.eduwrote:
Is there a function in JavaScript to get the list of the values for all
the 'id' attributes in an HTML page?

No.

You will have to sift through the elements on the page and find those
that have an id attribute with an appropriate value, e.g. where the id
attribute value has a length other than zero, something like:

if (element.id && element.id.length 0)
The second test is redundant and the first can throw exceptions.
Better to do this:

if (typeof element.id == 'string' && element.id)
Aug 1 '08 #5

P: n/a
Edgardo R. Del Rosario wrote:
Is there a function in JavaScript to get the list of the values for all
the 'id' attributes in an HTML page?
Thank you everybody... great suggestions! let me try them and I might
be talking to you soon! Thanks!
Aug 2 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.