Cannot detect click on mobile (text box)

To detect input click I am using a simple solution:

if (e.keyCode == 13) { // do something } 

On the desktop, it works fine. The tablet also has a smartphone - but if it comes to textarea, it won’t work.

This has become a problem after several recent chrome updates.

So, when you press enter from a mobile, you only see the new line "\ n", but not the execution of the function.

So how to detect textarea login on mobile devices on the latest version of chrome?

+5
source share
1 answer

While this may be a specific Chrome bug, there may be some possible solutions:

  • Also check e.which :

     var key = e.which || e.keyCode || 0; if (key == 13) { // do something } 
  • Try another event: for example. if you use keyUp try keyPress


If this still does not work, a workaround may be required for this if detection is important. You can track the value and detect as soon as "\ n" is added to the text field using the input event.

 var textarea = document.getElementById('your-textarea'); var textareaValue = textarea.value; var textareaLines = getLines(textareaValue); function getLines(str) { // This uses RegEx to match new lines. // We use || [] because it would otherwise fail if there weren't // any line breaks yet. return (str.match(/[\n\r]/g) || []).length; } textarea.addEventListener('input', function() { var newValue = textarea.value; var newLines = getLines(newValue); if (newLines > textareaLines && newValue.length == textareaValue.length + 1) { // We also check that the new length is only one // character longer to not fire this new line event // after a copy-paste with a new line character. doSomething(); } textareaValue = newValue; textareaLines = newLines; }, false); 
+1
source

Source: https://habr.com/ru/post/1243738/


All Articles