Sunday, November 23, 2008

Safari focus() at beginning of text instead of end of text

Safari browser has a weird textbox.focus() behavior. Instead of putting the cursor after text like the other browsers, it puts the cursor at the beginning of the text. My workaround for this problem to is use focus() then changing the value of the textbox.
element.focus();
element.value += '';

If the textbox needs to be changed anyway, just put the focus() right before changing the value.
element.focus();
element.value = 'some text';

*Using Safari 3.1.2