...

View Full Version : Passing an array to a function from a dynamic list



SumDood
06-13-2007, 01:39 PM
I'm having trouble passing data in an array to a javascript function. I dynamically create a list with edit links on each row and pass the array to the function on each link. Of course, the function only gets the data from the last row, regardless of which link is pressed. Here's the code:



<html>
<body>
<script language="JavaScript" type="text/JavaScript">
function populateTextboxes(propertyArray){
//populate text boxes with data from propertyArray:
document.formOne.tbOne.value = propertyArray[0];
document.formOne.tbTwo.value = propertyArray[1];
document.formOne.tbThree.value = propertyArray[2];
document.formOne.tbFour.value = propertyArray[3];
}
var propertyArray = new Array()
</script>

<!--following code generated via loop: -->

<script type="text/javascript">
propertyArray[0] = '1-one'
propertyArray[1] = '1-two'
propertyArray[2] = '1-three'
propertyArray[3] = '1-four'
</script>
<a href='#' onclick="populateTextboxes(propertyArray)" > Edit1 </a>&nbsp

<script type="text/javascript">
propertyArray[0] = '2-one'
propertyArray[1] = '2-two'
propertyArray[2] = '2-three'
propertyArray[3] = '2-four'
</script>
<a href='#' onclick="populateTextboxes(propertyArray)" > Edit2 </a>&nbsp

<script type="text/javascript">
propertyArray[0] = '3-one'
propertyArray[1] = '3-two'
propertyArray[2] = '3-three'
propertyArray[3] = '3-four'
</script>
<a href='#' onclick="populateTextboxes(propertyArray)" > Edit3 </a>&nbsp

<script type="text/javascript">
propertyArray[0] = '4-one'
propertyArray[1] = '4-two'
propertyArray[2] = '4-three'
propertyArray[3] = '4-four'
</script>
<a href='#' onclick="populateTextboxes(propertyArray)" > Edit4 </a>&nbsp


<form name="formOne" method="post" >
1<input type="text" name="tbOne" >
2<input type="text" name="tbTwo" >
3<input type="text" name="tbThree" >
4<input type="text" name="tbFour" >
</form>
</body>
</html>



Basically, I want to pass the row data to the function via an array so that I don't have to customize the function for each page on which I use it, as different pages have different number of elements. I'm definitely a script noob and I would be more than happy to implement a different work-around if what I am trying is too off the wall. Thank you in advance for your help.

Mr J
06-13-2007, 02:09 PM
Here's one possiblity


<script type="text/JavaScript">

propertyArray=[]

propertyArray[0] = ['1-one','1-two','1-three','1-four']
propertyArray[1] = ['2-one','2-two','2-three','2-four']
propertyArray[2] = ['3-one','3-two','3-three','3-four']
propertyArray[3] = ['4-one','4-two','4-three','4-four']

function populateTextboxes(n){ //populate text boxes with data from propertyArray:
document.formOne.tbOne.value = propertyArray[n][0];
document.formOne.tbTwo.value = propertyArray[n][1];
document.formOne.tbThree.value = propertyArray[n][2];
document.formOne.tbFour.value = propertyArray[n][3];
}

</script>

<a href='#' onclick="populateTextboxes(0)" > Edit1 </a>&nbsp
<a href='#' onclick="populateTextboxes(1)" > Edit2 </a>&nbsp
<a href='#' onclick="populateTextboxes(2)" > Edit3 </a>&nbsp
<a href='#' onclick="populateTextboxes(3)" > Edit4 </a>&nbsp

<form name="formOne" method="post" >
1<input type="text" name="tbOne" >
2<input type="text" name="tbTwo" >
3<input type="text" name="tbThree" >
4<input type="text" name="tbFour" >
</form>

SumDood
06-13-2007, 02:31 PM
Like a f'n charm. A two dimensional array, I should have thought of that!!! Efficient and elegant. Thanks a bunch!!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum