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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For loop question

Status
Not open for further replies.

rye8261

Technical User
Jul 6, 2003
359
0
0
US
I'm a php person not JS so I'm a little slow with JS. I have a php page that is in a for loop and will pass the javascript function a variable like "uniquename1" or "uniquename2"

My JS function looks like this, not pretty but works for me with a single variable (not with the .d parts). How can I get the function to accept a dynamic variable like the two above?

Code:
function ShowContent(d) {
if(d.length < 1) { return; }
	if(d == "uniquename".d){
	document.getElementById('empty'.d).style.display = "none";
	document.getElementById('hi5'.d).style.display = "none";
	document.getElementById('uniquename'.d).style.display = "block";
	}
	if(d == "hi5".d){
	document.getElementById('empty'.d).style.display = "none";
	document.getElementById('uniquename'.d).style.display = "none";
	document.getElementById('hi5'.d).style.display = "block";
	}
}

 
Well here is exactly what I have for my code now:
Code:
	<script type="text/javascript" language="JavaScript"><!--
function ShowContent(d) {
	
for (var x = 1; x <= 26; x++){
	if(d == "uniquename"+x){
	document.getElementById('empty'+x).style.display = "none";
	document.getElementById('hi5'+x).style.display = "none";
	document.getElementById('uniquename'+x).style.display = "block";
	}
	if(d == "hi5"+d){
	document.getElementById('empty'+x).style.display = "none";
	document.getElementById('uniquename'+x).style.display = "none";
	document.getElementById('hi5'+x).style.display = "block";
	}
}
//--></script>

I'm calling in in the php code with this where I is a number generated.
Code:
ShowContent('uniquename<?=$i?>');

So I'm trying to make the JS hide some divs like uniquename5 but don't know how to make the JS work for anything other than a name like uniquename hard coded.

 
This
Code:
if(d == "hi5"+d)

will never evaluate to True. Did you mean
Code:
if(d == "hi5"+x)
?

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top