how to find all elements starting with <text>

B

blaine

Hi,

I would like to find all elements within my DOM that begin with
"test". Any idea on how I would go about this?

Example Below I would like to return a list of element id's of test1,
test2, test3

<html>
<body>
<div id="spacer">
<div id="test1">Blah</div>
<div id="test2">Blah</div>
</div>
<div id="test3">Blah</div>
</body>
</html>
 
E

Evertjan.

Hi,

I would like to find all elements within my DOM that begin with
"test". Any idea on how I would go about this?

Example Below I would like to return a list of element id's of test1,
test2, test3

<html>
<body>
<div id="spacer">
<div id="test1">Blah</div>
<div id="test2">Blah</div>
</div>
<div id="test3">Blah</div>
</body>
</html>

Nice school assignmant, methinks.
Any idea on how I would go about this?

Sure, but it seeme better you explore the possiblilities yourself.

Advice: use a loop.
 
B

blaine

(e-mail address removed) wrote on 14 aug 2007 in comp.lang.javascript:







Nice school assignmant, methinks.


Sure, but it seeme better you explore the possiblilities yourself.

Advice: use a loop.

LOL.
No It was just a simple example of what I need to do.. Anyhow, I can
do this using a tree walker and reg exp.. However it does not work in
IE. If someone has a better solution, I would like to hear it.

function test(){

var treeWalker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_ELEMENT,
{ acceptNode: function(node) { return
NodeFilter.FILTER_ACCEPT; } },
false
);
var nodeList = new Array();
var exp = new RegExp("^test","ig");

while(treeWalker.nextNode()){
if (treeWalker.currentNode.id.match( exp )){
nodeList.push(treeWalker.currentNode);
}
}

var message = "";
for (var i = 0; nodeList.length > i; i++){
message += "\n" + nodeList.id;
}
alert( "LIST: " + message );

}

</script>
 
E

Evertjan.

LOL.
No It was just a simple example of what I need to do.. Anyhow, I can
do this using a tree walker and reg exp.. However it does not work in
IE. If someone has a better solution, I would like to hear it.

the TreeWalker object of DOM2, which is a DOM2 method supported in
Firefox/Opera8+ though not IE6 or IE7 (as of beta3).
<http://www.javascriptkit.com/dhtmltutors/treewalker.shtml>

Good that you found that script, but that does not mean it is not a
school assignment.

try:
document.getElementsByTagName(tagname)

Do you really need to inspect all elements of the DOM for thier ids?

Then it must be an assignment.
 
B

blaine

(e-mail address removed) wrote on 14 aug 2007 in comp.lang.javascript:







the TreeWalker object of DOM2, which is a DOM2 method supported in
Firefox/Opera8+ though not IE6 or IE7 (as of beta3).
<http://www.javascriptkit.com/dhtmltutors/treewalker.shtml>

Good that you found that script, but that does not mean it is not a
school assignment.

try:
document.getElementsByTagName(tagname)

Do you really need to inspect all elements of the DOM for thier ids?

Then it must be an assignment.

yes, I do want to inspect all elements for their id's.

Since what I am using this for is a sorting routing where I grab the
element object and some of it's attributes and sort by those. However,
the element in the DOM will not be sequentially labeled with something
link id="sort1" instead they will be like id="sort132324",
id="sort43243", etc.

Thus, get ElementsByTagName doesn't cut it..
 
E

Evertjan.

[please do not quote signatures on usenet]
yes, I do want to inspect all elements for their id's.

why? [For your example <div> is enough]

School assignment, methinks.
Since what I am using this for is a sorting routing where I grab the
element object and some of it's attributes and sort by those. However,
the element in the DOM will not be sequentially labeled with something
link id="sort1" instead they will be like id="sort132324",
id="sort43243", etc.

Thus, get ElementsByTagName doesn't cut it..

That is not logical.
You could inspect all <div>s for their id values,
without them having to be sequential.
 
R

RobG

Hi,

I would like to find all elements within my DOM that begin with
"test". Any idea on how I would go about this?

Example Below I would like to return a list of element id's of test1,
test2, test3

<html>
<body>
<div id="spacer">
<div id="test1">Blah</div>
<div id="test2">Blah</div>
</div>
<div id="test3">Blah</div>
</body>
</html>

Use getElementsByTagName('*'), loop through the elements, for those
with an ID, put the IDs (perhaps only the ones of interest) into an
array. Sort, slice, pop, whatever to your hearts content.

Keep an object handy with {id:elementRefernce,...} and use the array
as an index to get back to the elements - or just use getElementById.

Sounds like a bad idea, there must be another way to do whatever it is
you are trying to do.
 
E

Evertjan.

RobG wrote on 15 aug 2007 in comp.lang.javascript:
Use getElementsByTagName('*'),

Wow, simple! I forgot about the *

Place this at the bottom of a page:

<script type='text/javascript'>
var z = document.getElementsByTagName('*');
for (i=0;i<z.length;i++)
if (z.id)
document.write(z.tagName+': '+z.id+'<br>');
</script>
 
T

Thomas 'PointedEars' Lahn

I would like to find all elements within my DOM that begin with
"test". Any idea on how I would go about this?

Example Below I would like to return a list of element id's of test1,
test2, test3

<html>
<body>
<div id="spacer">
<div id="test1">Blah</div>
<div id="test2">Blah</div>
</div>
<div id="test3">Blah</div>
</body>
</html>

XPath makes this possible without having to iterate over the whole
D::gEBTN("*") NodeList. In Gecko-based UAs:

var snapshot = document.evaluate(
'//*[starts-with(@id, "test")]', document.body, null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null, null);

Then you can access the items of the snapshot with

snapshot.snapshotItem(num)

or you can iterate over a snapshot or an iterator list and
store the references in an array.

http://developer.mozilla.org/en/docs/DOM:document.evaluate


PointedEars
 

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
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top