Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

get selection source element



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?


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.. :-)

Featured Threads

Hot Threads This Week

Hot Threads This Month