0

This one is a strange one and I simply cannot get my head around it. We are trying to append space to a textarea content using the following jquery,

var checkThis ='help';
$('#msg').val(checkThis+' ');
$('#msg').focus();

But when we focus on the text area we can see that the space is not added. Also, the cursor doesn't focus automatically.

I am not able to figure out if there is any other script in the code doing this. This code is actually a large piece of maintenance code, is there anyway I can find what function may be trimming the text?

Cool Techie
  • 756
  • 2
  • 18
  • 39
  • Are you including jQuery correctly? Any errors in the console? This works: https://jsfiddle.net/y9hvc0k3/ – Andrew Brooke Dec 03 '15 at 18:57
  • Can you set up a non-working example, cause it sure seems like it works -> http://jsfiddle.net/g850adqw/ – adeneo Dec 03 '15 at 18:57
  • "I am not able to figure out if there is any other script in the code doing this." - and you think someone else who hasn't seen the code would be able to? One thought - is Angular involved at all - as ng-model auto-trims leading/trailing spaces. – James Gaunt Dec 03 '15 at 18:57
  • Actually this code is a piece of maintenance code that am looking at and it's huge.. i am almost sure that some other piece of code is causing this, but cannot search in the mess. Anyway i can reach where the trim might be happening? – Cool Techie Dec 03 '15 at 19:03
  • 1
    Attach a change handler, perhaps? – Rob Foley Dec 03 '15 at 19:08
  • You can try text() instead of val() if its just about a quickfix. Some info about that: http://stackoverflow.com/questions/8854288/val-vs-text-for-textarea `$('#msg').text(checkThis+' ').focus();` Why this thing is not focusing, no idea. – user5542121 Dec 03 '15 at 19:12

1 Answers1

0

Try this:

$('#msg').append(checkThis+' ').focus();
WhilseySoon
  • 322
  • 4
  • 14