Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

finding an element on a page

0 views
Skip to first unread message

rodchar

unread,
Oct 11, 2007, 11:39:03 AM10/11/07
to
hey all,
i know theres GetElementByID but is there a way to find an element by seeing
if just part of the id contains a certain string value?

thanks,
rodchar

darrel

unread,
Oct 11, 2007, 11:53:14 AM10/11/07
to
> i know theres GetElementByID but is there a way to find an element by
> seeing
> if just part of the id contains a certain string value?

Are we talking javascript here?

-Darrel


rodchar

unread,
Oct 11, 2007, 12:03:00 PM10/11/07
to
yes.

bruce barker

unread,
Oct 11, 2007, 12:22:23 PM10/11/07
to
sure, just walk the dom checking the id value of each node.

// generic dom walker

function domWalk(node,func)
{
var nodes = new Array();
if (func(node)) nodes[nodes.length] = node;
for (var i=0; i < node.childNodes.length; ++i)
{
nodes = nodes.concat(domWalk(node.childNodes[i],func));
}
return nodes;
}


// list of all elements whith 'mystring' in id
var list = domWalk(document.documentElement,function(n)
{
return (n.id && n.id.indexOf('mystring') != -1);
});
}

-- bruce (sqlwork.com)

rodchar

unread,
Oct 11, 2007, 2:01:01 PM10/11/07
to
thank you for this cool tip.
rod.

darrel

unread,
Oct 11, 2007, 2:07:48 PM10/11/07
to
> thank you for this cool tip.

That is a decent method. However, you might want to consider a
'getElementByClassName' function, in which case you could then 'tag' every
element with a class name and not have to deal with parsing the string.

-Darrel


rodchar

unread,
Oct 12, 2007, 8:42:01 AM10/12/07
to
thank you again.

rodchar

unread,
Oct 12, 2007, 2:43:02 PM10/12/07
to
i'm having a little trouble getting results because of my ignorance.
i put the function domWalk in the head section but i'm not sure where to put
the rest of the code:

> // list of all elements whith 'mystring' in id
> var list = domWalk(document.documentElement,function(n)
> {
> return (n.id && n.id.indexOf('mystring') != -1);
> });

i tried putting it right after the function but that doesn't seem to be
working for me.

rodchar

unread,
Oct 15, 2007, 12:31:01 PM10/15/07
to
ok, i think i figured out where things go now, but could someone please
explain in lamen's terms what is going on with the DomWalker. is this
considered a recursive
function? i don't think i've seen in javascript where a function passes
another function as a parm? that's kinda confusing??
0 new messages