javascript function get id value

  • Thread starter Edgardo R. Del Rosario
  • Start date
E

Edgardo R. Del Rosario

Is there a function in JavaScript to get the list of the values for all
the 'id' attributes in an HTML page?
 
R

RobG

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.
 
T

Thomas 'PointedEars' Lahn

Edgardo said:
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
 
T

Thomas 'PointedEars' Lahn

Thomas said:
[...]
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
 
D

David Mark

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)
 
E

Edgardo R. Del Rosario

Edgardo said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,814
Members
47,360
Latest member
kathdev

Latest Threads

Top