0

Today, I want automatic type the text to textarea

I don't want to change the value of textarea

$("textarea").text("abc")

The thing I want is simulate a keypress event like human. You can see this picture https://i.stack.imgur.com/edD9k.png

I tried trigger some keyboard events, but It doesn't work

var get_event = function(method, keycode){
  e = $.Event(method);
  if(keycode){
    e.which = keycode;
  }
  return e;
}

$('#test').focus();
$('#test').trigger(get_event("keydown", 65));
$('#test').trigger(get_event("keypress", 65));
$('#test').trigger(get_event("keyup"));
$('#test').trigger(get_event("blur"));
Khanh Le
  • 404
  • 4
  • 18
  • But now, i want to type by using javascript or jquery? i don't clear about your question, plz more clear – Omar Sep 26 '16 at 18:06
  • I don't get it... you have to type keyboard? What does that mean? How do you type by using javascript? Do you want to assign text to a textarea using jQuery? – Christine Sep 26 '16 at 18:07
  • @LittleZero You mean you want to programmatically fire key events? – ninjawarrior Sep 26 '16 at 18:07
  • ... or [simulate typing](http://stackoverflow.com/q/23688149/1169519) on a textarea? – Teemu Sep 26 '16 at 18:13
  • In the future, please don't come to StackExchange asking questions like this. This is an extremely BASIC problem that there are hundreds of tutorials and message boards and everything else that teach you how. I mean, I just searched "jquery assign textarea" and literally the first thing that came up was another StackExchange question that had a sufficient answer. This isn't a place to just come ask every little thing you think of. Think of it as a "last resort." – Christine Sep 26 '16 at 18:32
  • sorry everybody, but i think i did not describe enough so you seems not to understand me. Actually, i am trying to write a short code to post a status to a group on facebook. I have used Graph Api and everything is ok. But, the problem is the App_id which i use to send request has limit times to use. So the short code i am writting is just do jobs like we do when post a status: click to textarea, typing somethig, and click button post. The problem is if i change textarea value (this is so easy, and i dont want to ask for it), some other doms do not change as we typing. So, that is my question. – Khanh Le Sep 26 '16 at 23:23
  • @LittleZero I'd advice you to forget this question, it is not salvageable anymore. Instead, plan a new question carefully, before posting it. Try to ask what you really need, maybe add a code example of what you've tried to solve the proble. Put some effort to your question, and you will get an answer or two ... – Teemu Sep 27 '16 at 07:34
  • 1
    @Teemu: I want to simulate typing, but not by changing the value of textarea – Khanh Le Sep 27 '16 at 08:36
  • [Like this?](https://jsfiddle.net/rudwtbqd/) – Teemu Sep 27 '16 at 09:07
  • @Teemu: I don't want to use .value, It must do it automatically. please read more about my edited question – Khanh Le Sep 27 '16 at 09:17
  • No value at all? OK, I found [this SO answer](http://stackoverflow.com/a/23964375/), it doesn't have the code, but explains how this should be done. I was a bit lazy, and tried it with jQuery, but no luck. Give it a try with native JS event dispatching, maybe it works. – Teemu Sep 27 '16 at 11:02

1 Answers1

-1

Test the code below

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

<textarea class="test"></textarea>
<button onclick="addChar()">Add character</button>

<script type="text/javascript">

    var addChar = function (){
        $('.test').val($('.test').val() + "A");
    }

</script>
Marcin
  • 1,488
  • 1
  • 13
  • 27