Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp me improve this Javascript codes limitations?
    primarykey
    data
    text
    <p>This Javascript which uses functions from jQuery is quite handy but getting feedback on it there is some limitations which I was hoping you guys could help me overcome.</p> <p>The function basically creates a textbox with a formatted time (HH:MM:SS) so that it is easy for users to enter in times rather than have to use a date time picker which involves lots of clicks.</p> <pre><code>Code: //global variable for keeping count on how many times the function is called var i = 0; //for adding formatted time fields function timemaker() { //creates an input field of text type formatted with a value of "00:00:00" var input = $("&lt;input&gt;", { name: 'time'+i, // e.g. time0, time1, time2, time3 value: '00:00:00', maxlength: '8', size: '6' }); //this function which uses jquery plugin causes the cursor in the field to goto position zero //when selected making it easier for the user to enter times and not need to select the correct position input.click(function() { $(this).prop({ selectionStart: 0, selectionEnd: 0 }); //this child function moves the cursor along the text field //when it reaches the first ":" it jumps to the next "00" }).keydown(function() { if (event.keyCode == 9) { return; } else { var sel = $(this).prop('selectionStart'), val = $(this).val(), newsel = sel === 2 ? 3: sel; newsel = sel === 5 ? 6: newsel; $(this).val(val.substring(0, newsel) + val.substring(newsel + 1)) .prop({ selectionStart: newsel, selectionEnd: newsel }); } }); //this appends the text field to a divved area of the page </code></pre> <p>input.appendTo("#events"); i++; return; }</p> <p>00:00:00</p> <p><strong>Limitations I need help with</strong></p> <ul> <li>Say for example you wanted to enter a time of 12:45:00 , you obviously don't need to enter the seconds part (last "00") as they are already there. So you then decide to tab out of that text field but the javascript interprets your "Tab" keypress as an entry and deletes a zero from the field causing the value to be like <strong>12:45:0</strong></li> <li>Does not validate inputs for 24 hour format- do you think it will be possible to do that? e.g. first number you enter is "2" therefore the only options you have are "0,1,2,3"</li> <li>If you make a mistake in the 4th digit and reselect the text field you have to enter everything again.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload