views:

471

answers:

2

I'm trying to have the default behavior of a form select (or highlight) the text when you tab to the next input. I've had to add the .select() function on my password form fields for this to work in IE8. Is there an equivalent to the jquery .select() for deselecting text?

       $("#MyTextBox").focus(function() {
            $(this).select();
        });

        $("#MyTextBox").blur(function() {
            // Deselect here
        });
+1  A: 

Not that I am aware, however this ought to work for you:

$(this).val($(this).val);

Here you set the value of the field to itself. the cursor should automatically be put on the end.

James Wiseman
Brilliant! :) That did it.
Victor
A: 

The posted answer by James Wiseman worked perfectly in IE8, but for me at least, didn't work in Firefox 3.6.x.

I appreciate that the original question is specific to IE8, however, to help out those searching for a single solution that works in FF and IE, I used the following code:

var temptext = $(textbox).val();
$(textbox).val('');
$(textbox).val(temptext);

(where textbox is the textbox object to work with).

It uses the same theory as James Wiseman's solution, however, it adds the specific step of setting the textbox's text to a blank string prior to setting the textbox text back to the original string. Hope this helps!

CraigTP