4

I want to set focus into my text area. Following is my code:

$this.textInput.val('').show().focus();

But it is not working. Actually when I press mouse button it appeared but when I mouseup it remove from text area. So after lot of searching I found setTimout method like :

$this.textInput.mouseover(function(){                                
setTimeout($this.focus(),0);
});

But still its not working in firefox. I have the latest 13.0 version but still it containing the problem but google chrome it is working properly. What's the problem with firefox is there any solution for it.

Thanks in advance.

J.K.A.
  • 7,272
  • 25
  • 94
  • 163
  • reason of textarea loosing focus can be in other part of your code...a complete listing will help guys to understand the problem – shershen Jun 08 '12 at 10:07

4 Answers4

4

Use .trigger('focus'). I find it sometimes works better than .focus().

Sergei Zahharenko
  • 1,514
  • 12
  • 16
  • This. Also if you are going to use timeout set it to something low, but not 0 - around 10 will be fine: it will give the browser extra time if it needs it (Opera for example often does) and is short enough for user not to notice. – c2h5oh Jun 08 '12 at 10:08
  • and one more. I'm not sure if timeout still have $this when it's triggered. In that cases i use something like this: – Sergei Zahharenko Jun 08 '12 at 10:09
  • $this.textInput.mouseover(function(){ elem = $this; setTimeout(elem.trigger('focus'),0); }); – Sergei Zahharenko Jun 08 '12 at 10:09
  • Actually when I release the mouseclick it disappear from textarea. – J.K.A. Jun 08 '12 at 10:18
  • @acrashik : Thanks...its better...but in my case I am creating textarea dynamically over canvas which is perfectly working in google chrome but not in firefox – J.K.A. Jun 08 '12 at 10:29
2

Try this :

$('#textareaid').click(function(){
                $(this).after('focused?');
                el = $(this);
                setTimeout(function(){
                    el.trigger('focus')
                },1);
            })

Use .click method. I'll work for you.

J.K.A.
  • 7,272
  • 25
  • 94
  • 163
0

$this does not mean anything. You should use $(this) or you can set a variable like this-

var $this=$(this)
NewUser
  • 289
  • 2
  • 14
0

Not all elements are focusable but default, there is a tabindex attribute to fix that.

When you assign tabindex=<number> to an element:

It becomes focusable.

A user can use the tab key to move from the element with lesser positive tabindex to the next one. The exception is a special value tabindex="0" means that the element will always be last. The tabindex=-1 means that an element becomes focusable, but the tab key will always skip it. Only the focus() method will work

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335