Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Help


Message #1 by "Isaac Sogunro" <ilatine@h...> on Fri, 16 Feb 2001 21:20:37 -0500
Is it that you want the values from the array INSIDE the textboxes?? It 
still is not completely clear to me because you have "commented" out the 
tables. Anyway, if you want array values in the textboxes, here is a way to 
do it.

First the JavaScript. You can reference elements by their number, but also 
by their name. So what we do is take the value of i, add 1 to it (to get 
from the zero-based array to the 1-based textboxes) and add that vale to 
the word 'put'. So when i = 0 then sFieldName will have the value of 
'put1'. Then we reference the form (frmTest in this case) to get to the 
textbox and set its value.
In the HTML I haven't changed much. I have put the complete table inside a 
FORM so we can easily reference the elements on it, and I have put your 
attributes in quotes (well, my software did. It's usually a good practice 
to do so).

Hope this helps,

Imar


<script language="JavaScript">
var ArrTest = new Array(5);

ArrTest[0] = "Isaac";
ArrTest[1] = "Sarah";
ArrTest[2] = "Mike";
ArrTest[3] = "Henrietta";
ArrTest[4] = "Helen";


function button1_onclick()
{
         var i;
         for(i = 0; i <= 4; i++)
         {
                 var sFieldName;
                 sFieldName = 'put' + (i + 1);
                 document.frmTest(sFieldName).value = ArrTest[i];
         }
}
</script>

And in your HTML:

<form name="frmTest">
<table cellpadding="1" cellspacing="1" border="1" borderColor="orange">
<tr>
   <td><input type="text" name="put1" value="hey"></td>
   <td></td>
</tr>

<tr>
   <td><input type="text" name="put2" value="hey"></td>
   <td></td>
</tr>

<tr>
   <td><input type="text" name="put3" value="hey"></td>
   <td></td>
</tr>

<tr>
   <td><input type="text" name="put4" value="hey"></td>
   <td></td>
</tr>

<tr>
   <td><input type="text" name="put5" value="hey"></td>
   <td></td>
</tr>
</table>
<input type="button" value="Click me" language="JavaScript" onclick="return 
button1_onclick();">
</form>




At 09:20 PM 2/16/2001 -0500, you wrote:
>Hello.  I have sent this before but it did not come out right.  Also, I 
>was not clear.  Sorry.
>
>I built a table using HTML with 5 rows having 2 columns.  I also have an 
>array with 5 elements.  What I am trying to do is write a function that 
>will assign each element in the array, starting with 0, to the first cell 
>in each row correspondingly.
>
>When I bring it up in my browser, I get the error message: Syntax Error 
>line 19.
>
>Below is a piece of the code.  Please help if you can.  I am new to 
>javascript.
>
>Thank you.
>
>
>var ArrTest = new Array(5);
>
>ArrTest[0] = "Isaac";
>ArrTest[1] = "Sarah";
>ArrTest[2] = "Mike";
>ArrTest[3] = "Henrietta";
>ArrTest[4] = "Helen";
>
>function button1_onclick()
>{
>var i;
>  j = 1 //For the input name, since it's put1,put2,...etc
>  for(i=0; i<=5; i++)
>  {
>      document.name + "i" = ArrTest[j]; //This is line 19.
>      j++;
>  }
>}
>
>// -->
></COMMENT>
>
>
>//<table cellpadding=1 cellspacing=1 border=1 borderColor=orange>
>//<tr>
>//  <td><input type=text name=put1 value="hey"></td>
>//  <td></td>
>//</tr>
>
>//<tr>
>//  <td><input type=text name=put2 value="hey"></td>
>//  <td></td>
>//</tr>
>
>//<tr>
>//  <td><input type=text name=put3 value="hey"></td>
>//  <td></td>
>//</tr>
>
>//<tr>
>//  <td><input type=text name=put4 value="hey"></td>
>//  <td></td>
>//</tr>
>
>//<tr>
>//  <td><input type=text name=put5 value="hey"></td>
>//  <td></td>
>//</tr>
>//</table>
>//<input type=button language=javascript onclick="return button1_onclick()">


  Return to Index