homepage Welcome to WebmasterWorld Guest from 54.145.183.190
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
focus on next textbox after entering restricted number of chars
snehula



 
Msg#: 4342694 posted 1:47 pm on Jul 22, 2011 (gmt 0)

Hi again :-)

I was asked to change the way user enters a 12 digit number, to spread it across 3 textboxes for better readability, with automatically moving to next textbox when the permitted number of chars/textbox is reached. I have a function called on an onkeyup event of each of the textboxes, the function looks like this:

function next(obj) {
if(obj.value.length == 4) {
var nextnode, target;
nextnode=obj;
nextnode=nextnode.nextSibling;
while(nextnode) {
if (nextnode.nodeType==1) {
target=nextnode;
break;
}
nextnode=nextnode.nextSibling;
}
if (target) {
target.focus();
}
else {}
}
}


This works fine as long as there is nothing in the textboxes originally. The trouble is, in some cases, I need to retrieve that 12 digit value from a db, display it in the textboxes and then let the user change it if they want. In this case, I have set the onfocus event of the textboxes to onfocus="this.select();", so that once the user enters a textbox, he doesn't need explicitly delete its contents before typing something new.
As soon as any of them textboxes get focus, the above code causes the focus to move to the next textbox immediately since the length is already 4. I have tried to change the code to:
if(obj.value.length == 4 && document.selected == undefined)

to make sure it's only executed if the text in the box is not selected any more(and the user actually started typing), but it's useless.. I was wondering if a former listbox selection could interfere with it (giving document.selected other value than undefined) but I get the feeling that the problem lies somewhere else.. Help! Hilfeeeee! :-)

 

snehula



 
Msg#: 4342694 posted 1:51 pm on Jul 22, 2011 (gmt 0)

i'm using FF and the mysterious thing is, that it actually works if I click into the first of those textboxes. Works like a charm. But if i tab my way into the textbox, the thing with the tboxjumping happens... I'm really lost here :-]

snehula



 
Msg#: 4342694 posted 10:46 am on Aug 3, 2011 (gmt 0)

okay forget it..

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved