Web Analytics Made Easy -
StatCounter How to bring focus on an Input field when I clone it this way. - CodingForum

Announcement

Collapse
No announcement yet.

How to bring focus on an Input field when I clone it this way.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to bring focus on an Input field when I clone it this way.

    I am using this code so when I press Enter, it adds another input field below.

    My problem is thatI want the newly generated field to be focused as well.

    This is my code. (You may copy & paste it in an .html file and run it straight away)

    Code:
    <script language="javascript">
    
     function insRow(row)
    {	i=row.parentNode.parentNode.rowIndex;
    	var errorFlyer = document.getElementsByTagName('errorFlyer');
            var x=document.getElementById('invTable');
            var new_row = x.rows[i].cloneNode(true);
    
            var len = x.rows.length;
            new_row.cells[0].innerHTML = len+1;
    	
    	var subj = new_row.cells[1].getElementsByTagName('input')[0];
      	subj.value = '';
       
     x.rows[i].parentNode.insertBefore(new_row, x.rows[i].nextSibling);
    
    }
    
    function runScript(e, row) {
        if (e.keyCode == 13) {
    		insRow(row);
            return false;
        }
    }
    
    </script>
    
    <form id="form1" name="form1" method="post" action="">
    <table id="invTable">
    <tr>
              <td>1</td>
              <td>Title: <input size=100 type="text" name="Subject[]" value = "[[Subject]]" onkeypress="return runScript(event,this)"/>
              <td><input type="button" id="addInv" value="Add" onclick="insRow(this)"/></td>
               </td>
    </tr>
    </table>
    </form>

  • #2
    I think I got it my self.

    I added these lines:
    subjname = document.getElementsByName("Subject[]");
    subjname[i+1].focus();

    at the end of the function insRow(row)

    Comment

    Working...
    X