Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
passing string to function using assigned functions
Old 06-22-2006, 01:27 PM passing string to function using assigned functions
funkdaddu's Avatar
Web Design Snob

Posts: 635
Trades: 0
This is a simplified version of my script. Basically at load I want to run through an array of object ids, and assign functions to them to open drop down menus. The problem I'm having is that the parameters that are being passed to the function are being sent as the last instance of the variable, not the one assigned at the time of assigning the function. Example:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

	<head>
		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
		<title>Untitled Page</title>
		<script type="text/javascript"><!--
var buttons = new Array("btnOne","btnTwo");

window.onload = function() {
	for (x in buttons) {
		btnName = buttons[x];
		btnBox = "DD" + btnName;
		document.getElementById(btnName).onclick = function() {
			document.getElementById(btnBox).style.display = "block";
		}
	}
}

//-->
</script>
	</head>

	<body bgcolor="#ffffff">
		<p><button id="btnOne" name="btnOne" type="button">Button1</button></p>
		<p id="DDbtnOne" style="display:none">This is button 1's Text</p>
		<p><button id="btnTwo" name="btnTwo" type="button">Button2</button></p>
		<p id="DDbtnTwo" style="display:none">This is button 2's Text</p>
	</body>

</html>
Basically this assigns an function that shows a div when you click the button. Now when you click either it shows #2's text, because at the time of clicking the button, btnBox = "DDbtnTwo" as it was the last thing in the loop. How can I have it assign a string based on the variable at the time of assigning the varible, not at the time the user clicks on it? Basically doing this:

Code:
document.getElementById("DDbtnOne").onclick = function() {
			document.getElementById("DDbtnOne").style.display = "block";
		}
document.getElementById("DDbtnTwo").onclick = function() {
			document.getElementById("DDbtnTwo").style.display = "block";
		}
But being able to loop through it. Any ideas? Thanks.

Last edited by funkdaddu; 06-22-2006 at 01:29 PM..
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
 
Register now for full access!
Old 06-22-2006, 01:37 PM Re: passing string to function using assigned functions
funkdaddu's Avatar
Web Design Snob

Posts: 635
Trades: 0
Heh, I figured out a work-around. I just used:

document.getElementById("DD"+this.id).style.displa y = "block";

though if there's another way to do it, I'd like to know. Thanks.
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Reply     « Reply to passing string to function using assigned functions
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23504 seconds with 12 queries