1

Not sure why this doesn't work in Firefox (works fine in Chrome). Basically trying to focus on the current textarea with id "test" after making a change to the text.

<textarea id="test"></textarea>
<textarea id="test2"></textarea>

And JQuery code:

$('#test').change(function() {
    alert("You entered: "+$(this).val());
    $(this).focus();
});

Refer to: http://jsfiddle.net/e0fahvh0/

This is not a duplicate, as the questions asked before (refer to Calling $().focus on a textarea in Firefox doesn't work as expected) was via a click event, this is a change event and it's somehow different!

Community
  • 1
  • 1
guowy
  • 239
  • 2
  • 16
  • By the way use `$(this).val()` instead of `$(this).text()` – Dan Aug 21 '14 at 03:05
  • Thanks @Dan you're right LOL, I only quickly put this together for debugging purpose :P I've updated it to .val() – guowy Aug 21 '14 at 03:07
  • This might provide an answer http://stackoverflow.com/questions/5278178/calling-focus-on-a-textarea-in-firefox-doesnt-work-as-expected – Dan Aug 21 '14 at 03:10
  • @Mahan did you try it in Firefox? – guowy Aug 21 '14 at 03:21
  • @Dan Thanks, I saw that one before posting mine, if you try that in JSFiddle, it still doesn't work :( – guowy Aug 21 '14 at 03:22
  • @guowy I tried it on firefox, I replicated your problem. please try my answer below and tell if that's the behaviour you want – Netorica Aug 21 '14 at 03:25

1 Answers1

1

we just need to create a delay to give the focus on the textarea

$('#test').change(function() {
    var ttt = $(this);
    setTimeout(function(){
         alert("You entered: "+ttt.val());
         ttt.focus();
    },50);

});

http://jsfiddle.net/dL442hjo/1/

Netorica
  • 18,523
  • 17
  • 73
  • 108
  • Thanks, but did you test your answer in JSFiddle? The alert prompt kept coming up, that's not the effect I'm trying to achieve. – guowy Aug 21 '14 at 03:48
  • yeah, what are you trying to achieve here? do you want to have the alert after all the changes was made? are you trying to make a character counter? – Netorica Aug 21 '14 at 03:56
  • I have an edited my fiddle – Netorica Aug 21 '14 at 04:00
  • Thanks, using the setTimeout seems to be able to hack it through Firefox :) Not ideal, but it does resolve this matter. Thanks :) – guowy Aug 28 '14 at 00:39