12/31/2009

IE: change the value of a text input box on focus

IE has a strange behavior of setting the cursor to the beginning of the input box if you change the value of a text input on focus like this (using jQuery in this example):

$('#inputbox').focus(function() {$(this).val('somevalue')})

The way around this is to use event bubbling. You can use this script to allow jQuery live function to work with focus event: http://stackoverflow.com/questions/1199293/simulating-focus-and-blur-in-jquery-live-method (jQuery 1.4 and above should have this built in). So the correct code will look like:

$('#inputbox').live('focus', function() {$(this).val('somevalue')})

IE will not put the cursor in the exact position where user would expect it to be.

0 comments: