views:

1128

answers:

4

I understand that with javascript you can select the contents of a textbox with the following code (in jQuery):

$("#txt1").select();

Is there a way to do the opposite? To deselect the content of a textbox? I have the focus event of a series of textboxes set to select the contents within them. There are times now that I want to focus a particular textbox WITHOUT selecting it. What I am planning on doing is calling the focus event for this particular textbox, but then follow it with a call to deselect it.

$("input[type=text]").focus(function() {
    $(this).select();
});

//code....

$("#txt1").focus();

//some code here to deselect the contents of this textbox

Any ideas?

Thanks!

+2  A: 

If you just assign the value of the textbox to itself, it should deselect the text.

Brandon
A: 

Rather than selecting and then deselecting, why not just temporarily store a boolean on the dom element?

 $("input[type=text]").focus(function() {
     if($(this).skipFocus) return;
     $(this).select();
 });

 //code....

 $("#txt1").skipFocus = true;
 $("#txt1").focus();
Tom Ritter
A: 

If you want to deselect a text box using jQuery do the following:

   $(your_input_selector).attr('disabled', 'disabled');
   $(your_input_selector).removeAttr('disabled');
James Lemieux
A: 

what about this:

$("input").focus(function(){
  this.selectionStart = this.selectionEnd = -1;
});
mkoryak