finding an element on a page

G

Guest

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
 
D

darrel

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
 
B

bruce barker

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,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)
 
G

Guest

thank you for this cool tip.
rod.

bruce barker said:
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,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)

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
 
D

darrel

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
 
G

Guest

thank you again.

darrel said:
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
 
G

Guest

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.


bruce barker said:
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,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)

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
 
G

Guest

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??


bruce barker said:
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,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)

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
 

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

Similar Threads


Top