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 7 of 7
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to transfer value from Java arrays to Javascript and loop thru to get the values

    I am trying to loop through a java array using javascript and store the value in the textbox named txtAAESNo. But i am geting the following error:
    value "i" cannot be resolved.

    Pls help!

    Code:
    function hello()
    {			
    
      var i;
    	for( i = 0; i < <%=arrAccSec.length%>; i++)
    		{
    	document.getElementById('txtAAESNo').value=<%=arrAAFS[i][1] %>;
    				
    			
    		}
    			
    	
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Try escaping the < and > to \< and \>

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Apr 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Not helping! the function itself has stopped executing..

  • #4
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Code:
    <%=arrAccSec.length%>
    indicates to me you are trying to use JS with a asp array of some kind. You cant iterate through a server side array in client side code. If it didd somehow work, then you would be just overwriting the value multiple times and only end up with the last value found by the loop.
    Last edited by DanInMa; 08-17-2013 at 04:00 PM.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    var txt = document.getElementById('txtAAESNo');
    
    <% for (int i=0; i < arrAAFS.length; i++) { %>
           txt.value = '<%=arrAAFS[i][1]%>';
    <%}%>
    But as DanInMa pointed out, you are overwriting the textbox value. So you'll end up with the last value.

    Note: There might be a typo because I'm typing on the phone.
    Last edited by glenngv; 08-17-2013 at 06:19 PM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,940
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    DanInMa: This is JSP code, not ASP code. JSP uses the same <%...%> notation that ASP uses. PHP had to be different and use <?...?>.

    Glenn's code is correct. But as he says, it is kind of pointless.

    Why assign a different value to the same <input> over and over.

    Just do this, instead:
    Code:
    document.getElementById("txtAAESNo").value = "<%=arrAAFS[arrAAFS.length-1][1]%>";
    The end result is the same.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Maybe the OP wanted all the elements in the array to be in the textbox?
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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