views:

590

answers:

3

Hi all,

I have the following problem. In text input filed which work as an auto-completer some of suggestions it returns are wither than it. The problem comes when you leave the field. In IE the text cursor is positioned on the end of the suggested string, so you can actually see only the last part of it. So I used the code bellow to fix this and it works under IE6, but in IE8 this doesn't work, the field is always selected and I can not select anything on the page.

My question is what is the right way to move the cursor in the beginning of input field, after I leave it?

$('#myAutocompleter').blur(function(){
  textRange = this.createTextRange();
  textRange.collapse(true);
  textRange.select();
});

(The used code is written in jQuery.)

A: 

I'm not sure I understand your question, but IE has it's own set of methods for handling text selection on a page, so that would be why it behaves differently.

Look here for a tutorial: http://www.quirksmode.org/dom/range%5Fintro.html

And here for compatability: http://www.quirksmode.org/dom/w3c%5Frange.html

If that's not your problem, try doing a mouseup or click event check inside blur and putting the selection code there? Maybe that'll cause the selection to move away from the input field before placing it where the selection is.

Matrym
My question in general is how IE to align the text into "text input filed" in left after I leave the input. In Mozilla FF this is handled by the browser but in IE it's not. I tried to do it with code in my post but it's not working right.
bozhidarc
A: 

I had a similar situation where I wanted to see the top/bottom of something depending upon things like this. I used the jQuery scrollTo plugin

).scrollTo('100%')

).scrollTo('0%')

EDIT 1: I am using it on this field:

<textarea cols="57" rows="2" class="cssTextarea cptEntryArea"></textarea>

with this code:

  $(".cptEntryArea").change(function()
        {
           $(this).scrollTo('0%');
   });
Mark Schultheiss
I'm exactly not sure how your situation is like mine, but scrollTo() can not be used on a text field.
bozhidarc
+1  A: 

I believe what you're looking for are the .moveStart and .moveEnd methods of the text range:

$('#myAutocompleter').blur(function(){
  textRange = this.createTextRange();
  textRange.collapse(true);
  textRange.moveEnd('character',0);
  textRange.moveStart('character',0);
  textRange.select();
});

(Tested functional in IE8)

T. Stone