Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing an array to a function from a dynamic list

    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:

    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.

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Here's one possiblity

    Code:
    <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>
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Like a f'n charm. A two dimensional array, I should have thought of that!!! Efficient and elegant. Thanks a bunch!!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •