473,479 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Advancing to next field

Claus Mygind
571 Contributor
I am creating a table dynamically adding lines to the table as the user completes each line. I would like the cursor to appear in the second cell of the new row after it has been created, but I seem to have some problems with that. At this point I am not even using the variable cNextLine I am just trying to advance to the 2nd cell in the 2nd line. The focus seems to go to the address box of the browser.


Here is an abbreviated version of the code:

Expand|Select|Wrap|Line Numbers
  1. var cNextLine = 1
  2. function addBlankDetailLine()
  3. {
  4.     cNextLine += 1
  5.     var tr, td;
  6.     tbody = document.getElementById('detailLines');
  7.  
  8.     //make new row
  9.     tr = tbody.insertRow(tbody.rows.length);
  10.  
  11.     //make new cell
  12.     td = tr.insertCell(tr.cells.length);
  13.     //fill in content
  14.     td.innerHTML =    '<input '+
  15.                     'type="text" '+
  16.                     'id  ="rLine~'+cNextLine+'" '+
  17.                     'name="rLine~'+cNextLine+'" '+
  18.                     'value="'+cNextLine+'." '+
  19.                     'size ="4" '+
  20.                     'class="InputText" '+
  21.                     'readonly '+
  22.                     'tabindex="-1" '+
  23.                     '/> '
  24.     //make new cell
  25.     td = tr.insertCell(tr.cells.length);
  26.     td.innerHTML =    '<input '+
  27.                     'type="text" '+
  28.                     'id  ="JOBID~'+cNextLine+'" '+
  29.                     'name="JOBID~'+cNextLine+'" '+
  30.                     'class="InputText" '+
  31.                     '/> '
  32.  
  33.     //make new cell
  34.     td = tr.insertCell(tr.cells.length);
  35.  
  36.     td.innerHTML =    '<input '+
  37.                     'type="text" '+
  38.                     'id  ="TOLLS~'+cNextLine+'" '+
  39.                     'name="TOLLS~'+cNextLine+'" '+
  40.                     'class="InputText" '+
  41.                     'size ="8" '+
  42.                     'maxlength="7" '+
  43.                     'onblur ="if (createNext(this)) {addBlankDetailLine();}" +'
  44.                     '/>'    
  45. document.getElementById("JOBID~2").select;
  46. document.getElementById("JOBID~2").focus();
  47.  
  48. }
  49.  
  50.  
Sep 15 '08 #1
5 1325
Atli
5,058 Recognized Expert Expert
You could add the element using createElement and appendChild, rather than using innerHTML.

Then you would have a reference to the element in a variable, which you could use to set the focus.

Like:
Expand|Select|Wrap|Line Numbers
  1. input = document.createElement("input");
  2. input.type = "text";
  3.  
  4. cell = row.insertCell(row.cells.length);
  5. cell.appendChild(input);
  6.  
  7. input.focus();
  8.  
Sep 16 '08 #2
rnd me
427 Recognized Expert Contributor
you have "2" hard-coded into the function that sets focus.

remember that you cannot re-use ids, they must be unique.

thus, you are always trying to focus the same input.
since #2 has not been made the first time; cLine= 0 or 1; you are trying to focus an element using a non-existent id.
Sep 16 '08 #3
Claus Mygind
571 Contributor
you have "2" hard-coded into the function that sets focus.

remember that you cannot re-use ids, they must be unique.

thus, you are always trying to focus the same input.
since #2 has not been made the first time; cLine= 0 or 1; you are trying to focus an element using a non-existent id.

Thank you for your response, but as noted in my initial response I simply hard coded in the 2 for test purposes and was only looking to get focus on the 2nd line, which I could not do.
Sep 22 '08 #4
Claus Mygind
571 Contributor
Thank you for your reply.

I tried to implement it but came up with two problems.
1) not all the attributes took
2) it still did not advance to the desired field

Here is my code.

Expand|Select|Wrap|Line Numbers
  1.  
  2.     td = tr.insertCell(tr.cells.length);
  3.  
  4.     inputX = document.createElement("input");
  5.     inputX.type = "text";
  6.     inputX.id   = "JOBID~"+cNextLine;
  7.     inputX.name = "JOBID~"+cNextLine;
  8.     inputX.class="InputText";
  9.     inputX.size ="7";
  10.     inputX.maxlength="6";
  11.     inputX.onKeyPress="this.value = this.value.toUpperCase();    return loadPhaseCoder(event, this); return isValidCode(event, this);";
  12.     inputX.onchange ="if (cSetLoc(this)) {isNotEmpty(this); getPrjct(this); DataChanged();}";
  13.  
  14.     td.appendChild(inputX);
  15.  
  16. //////////////////////
  17. additional cells inserted
  18. /////////////////
  19.  
  20.  inputX.focus();
  21.  
  22.  
the output should have looked like this

Expand|Select|Wrap|Line Numbers
  1. <input id="JOBID~1" class="InputText" type="text" onchange="if (cSetLoc(this)) {isNotEmpty(this); getPrjct(this); DataChanged();}" onkeypress="this.value = this.value.toUpperCase(); return loadPhaseCoder(event, this); return isValidCode(event, this);" maxlength="6" size="7" name="JOBID~1"/>
  2.  
But this is what I got

Expand|Select|Wrap|Line Numbers
  1. <input id="JOBID~2" type="text" name="JOBID~2" size="7"/>
  2.  
Sep 22 '08 #5
Atli
5,058 Recognized Expert Expert
Ok.

To set the class, you should use the className property. It works on all the major browsers.

For the other properties that don't work, you could try using the setAttribute function.
Like:
Expand|Select|Wrap|Line Numbers
  1. elem.setAttribute("maxlength", "6");
  2.  
The events, however, are a little more complex.
You need to add them using the addEventListener function. (Or attachEvent for IE).

That should be done somewhat like this:
Expand|Select|Wrap|Line Numbers
  1. // Create the callback function
  2. callback = function() {
  3.     alert("From the onclick event");
  4. };
  5.  
  6. // Add the event
  7. if (window.addEventListener) { // Standard method
  8.     elem.addEventListener("click", callback, true);
  9. }
  10. else if(window.attachEvent) { // IE workaround
  11.     elem.attachEvent("onclick", callback);
  12. }
  13.  
Note the difference between the Standard method and how IE implements it.
IE also requires that the event is prefixed with "on".
Sep 22 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1342
by: Tim Smith | last post by:
In my .cs code I populate set the DataSource appropriately and I can get my DataGrid to appear, though sorting throws an error 'dgDataGrid1__ctl1__ctl0' of type 'DataGridLinkButton' must be placed...
1
948
by: Tim Smith | last post by:
In my .cs code I populate set the DataSource appropriately and I can get my DataGrid to appear, though sorting throws an error 'dgDataGrid1__ctl1__ctl0' of type 'DataGridLinkButton' must be placed...
2
3103
by: henrywuxg | last post by:
Hi All, I would like to use Period Key(.) besides Tab to toggle from one textbox to another when user is editing IP addresses, which have four textboxes. The function in below works fine with...
6
13540
by: Rig20 | last post by:
Hello, I’m sure this is a relatively easy fix, but I have an unbound combo box on a form that runs numerous queries in the After Update section. Everything there works fine. All I need to do is...
11
2471
lee123
by: lee123 | last post by:
if you have a list box with music in them how do you have it advance to the next one down to the next song when the one is over? lee123
0
7027
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6899
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7067
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6719
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6847
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4463
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
166
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.