最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript Event Listener for Text typed in Text FieldArea - Stack Overflow

programmeradmin0浏览0评论

Is it possible to use JavaScript to listen for text typed in a specific text-field, and then grab that text and save it to a string. Well, the latter (e.g., saving to string) will of course be possible if the former can be done.

I found a Java Applet that does this (.0/ui/textlistener.html), but I was wondering if the same could be done with JS.

I'm just looking for some resources/docs.

If anyone can help, I'd much appreciate it!

Is it possible to use JavaScript to listen for text typed in a specific text-field, and then grab that text and save it to a string. Well, the latter (e.g., saving to string) will of course be possible if the former can be done.

I found a Java Applet that does this (http://journals.ecs.soton.ac.uk/java/tutorial/post1.0/ui/textlistener.html), but I was wondering if the same could be done with JS.

I'm just looking for some resources/docs.

If anyone can help, I'd much appreciate it!

Share Improve this question edited Nov 7, 2022 at 22:18 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 8, 2011 at 12:37 ArtSabintsevArtSabintsev 5,20011 gold badges43 silver badges72 bronze badges 1
  • 1 problem here : you won't be able to catch if the user keep touch down. So if he enters "aaaaaaaaaaaaa" you'll catch it only when he'll release the "a" key. – remi bourgarel Commented Sep 8, 2011 at 12:47
Add a ment  | 

3 Answers 3

Reset to default 4

simple with jQuery

$('input[name=your-input]').change( function () {

    alert(this.value); //or alert( $(this).val() );

    });

I'd remend trying JQuery which will allow you to bind to the keydown event of any element you select: http://api.jquery./keydown/

However, you might want to only update the string after the user has finished typing, so you might want to consider updating it whenever the textfield loses focus - which you can do by binding to the blur event in the same way.

EDIT: Yes - realise I was being a bit stupid here. change is the event you should bind to for this.

In vanilla javascript you add an event listener:

document.getElementById('your-input').addEventListener('input', ()=>{
 
  console.log(this.value);

})
发布评论

评论列表(0)

  1. 暂无评论