#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    134
    Rep Power
    10

    Retrieve cell location in table


    I have a table and I am trying to enter values into the table without the need to press the tab key or enter key after each entry. So far I have code that enters the value and simply acknowledges this with an alert message (for testing purposes only). I then want to automatically move the cursor to the next cell below the one completed. At the moment the code simply moves the cursor to the right hand cell in the second row as I do not know how to pick up the current cursor position and modify it to move the cursor down one row.

    I have tried to use document.getElementById and rowIndex but am struggling to understand this and to therefore retrieve the row number. So, I want to know how to retrieve the row number when the function is executed please. The demonstration code is...

    Code:
    <head>
    
    <script type="text/javascript">
    
     function doNext(x)
     {
    alert ("ok")
    document.fff.e_2.focus()
     }
    
     </script>
    
    
    </head>
    
    <body bgcolor="#FFFFFF">
    <?php 
    global $i;
    
    $ff=array("0","1","2","3","4","5","6","7","8","9","10");
    	$form = "<form name = \"fff\" >";  
        $form.= "<table  id=\"tblMain\" border=\"1\"  cellspacing=\"0\" cellpadding=\"0\"> ";
    	$form.= "<tr>";
        $form.= "<td width = \"10\" align = \"center\">Num</td> ";
        $form.= "<td width = \"10\" align = \"center\">Score</td> ";
    	$form.= "</tr>";
    for ($i = 1;$i<=10;$i++)
    	{	//AAAAAA
    $yy = "tr".$i;
    $form.="<tr  id = \"$yy\" >";
    	$val_a2 = $ff[$i];
        $form.= "<td width = \"10\" align = \"center\">".$val_a2."</td> ";
    $val_e1 = "e_".$i;
    $form.= "<td width=\"10\" align =\"center\"  ><input type=\"text\" id = 'ss' name= ".$val_e1." value = \"\"  style='text-align: center;' onkeyup=\"doNext(tblMain);\" ></td> ";
        $form.= "</tr>";
    	}	//AAAAAA
        $form.= "</table>";
        $form.= "</form>";
    echo ($form);
    
    ?>  
    
    </body>
  2. #2
  3. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,896
    Rep Power
    9646
    Question: how is the code supposed to know when you're done typing and ready to move to the next one?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    134
    Rep Power
    10
    When you run this code and enter a number (score) in the second column the "onkeyup" command is executed and "ok" is displayed as an alert message. Because of the way that I have written the code at the moment the cursor moves to the 2nd row (irrespective of which row you have entered the value). I want it to move to the next row below the one that I have just entered a value. So I want to retrieve the row number and then modify the command "document.fff.e_2.focus()" to state e_2 if you entered a value in the first row, or e_3 if you entered a value in the 2nd row etc.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    134
    Rep Power
    10
    Okay - I appear to have resolved this one.

    I changed the reference/link in the Form from
    PHP Code:
    onkeyup=\"doNext(tblMain);\" 
    to

    PHP Code:
    onkeyup=\"doNext(".$i.");\" 
    and changed the Javascript function to

    Code:
     function doNext(x)
     {
    x=x+1
    xx = "e_"+x
    document.forms[ 'fff' ].elements[ xx ].focus();
    //document.fff.e_2.focus()
     }
     </script>

IMN logo majestic logo threadwatch logo seochat tools logo