Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a wildcard to use?

Status
Not open for further replies.

fedtrain

Instructor
Joined
Jun 23, 2004
Messages
142
Location
US
If I need to find all the layers that start with a certain value...regardless of the rest of the name...how do I do that?

What I am thinking is something like this:

for (var x=0, x< ,x++)
document.getElementByID(sec+ *)[x].visibility='hidden'

Now, I have no idea what to put in the x< spot, and I have no idea if the wild card option is available.

It seems there is a way to use regular expresions, but can you put a variable you have already created into a regular expression?

Dave

"Credit belongs to the man who is actually in the arena - T.Roosevelt
 
You can use

var divs = document.getElementsByTagName('div');

which returns an array of the objects that are divs. Then you can go through those elements and check for the common id you want:

for (var di=0; di < divs.length; di++)
{
if (div[di].id.indexOf('sec') == 0)
{
do what you want to do
}
}

Is this the kind of thing you want?

Lee
 

fedtrain,

Hopefully my answer in your other thread will do the job for you.

Hope this helps,
Dan

 
Hey Lee,
Well, just looking at it...it seems close but I guess I am not sure what to do with it. I seem to just be creating confusion all over the last couple of days.

Lets say I have four layers.

1) id = '1right'
2) id = '1wrong'
3) id = '2right'
4) id = '2wrong'

When the student answer the answer in the pop-up, that answer determines which layer is shown. If they have answered a question for sec 1, then I want to find all the divisions for section one, regardless of the answer part, and make them hidden. So I need to search for ids that start with 1 and then *, or some wildcard or something.

Hope that helps with why I am looking for some kinda wildcard. But then I may be chasing this all wrong by now.

Dave



"Credit belongs to the man who is actually in the arena - T.Roosevelt
 
You could use a function like this to find all elements with a certain string in the id:
Code:
function getElementsWC()
{
els = new Array();
if (arguments[0].indexOf("*")==-1) return document.getElementById(arguments[0]);
else { 
for (x=0;x<document.getElementsByTagName("*").length;x++)
	{ 
	if (document.getElementsByTagName("*")[x].id.indexOf(arguments[0].split("*")[0])!=-1){
	els[els.length]=document.getElementsByTagName("*")[x];}
	}
return els;
}
}
Then a function like this to hide those divs:
Code:
function showEm()
{
divs = getElementsWC(arguments[0]);
for (x=0;x<divs.length;x++) {divs[x].style.display="block";}
}
You would call it thusly: [tt]showEm("1*")[/tt] to reveal all the layers with an id beginning with "1".

hope that helps

"It is the mark of an educated mind to be able to entertain a thought without accepting it." - Aristotle
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top