homepage Welcome to WebmasterWorld Guest from
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

get selection source element

Msg#: 4373649 posted 3:07 pm on Oct 12, 2011 (gmt 0)


I have the following function executed on an onkeyup event of a textbox:

function next(that, num) {
if(document.selection || window.getSelection() || document.getSelection()) {//do nothing}
else { //if there is 'num' number of chars entered in the textbox, jump to next textbox}

The goal is to get the cursor moved from textbox A to textbox B as soon as the user enters 3 characters. But! There might be a value in textbox A initially, so the cursor jumps to textbox B as soon as A gets focus, since there is 3 chars in it already. To avoid this, i thought it might work if I do a this.select() on the onfocus event of textbox A and then in the function 'next', only execute the cursor jumping code if the text is not selected (that is, after the user rewrites it).
The problem is, that irrespective of textbox A, i do have another selection on the form, a listbox option being highlighted as default, so the else clause never gets executed. I really couldn't find a way to identify which element a selection belongs to and write the condition based on that. This is probably quite stupid of me, but anyway.. i tried something like 'that.selection' instead of document.selection but this is not valid. Am I missing something related to selections or should i try an entirely different approach?



Msg#: 4373649 posted 4:43 pm on Oct 12, 2011 (gmt 0)

okay, little bit of experimentation led to the following working replacement of the if condition:
if(that.selectionStart != that.selectionEnd)

I would still be interested though, if there's a better way to do this or if there's any troubles with what i've done here.. :-)

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