简体   繁体   中英

How to press Enter without actually touching keyboard, using JavaScript, inside an input/textbox box

I know this has been asked many times and I searched a lot. But solutions are either not working in IE or not meeting my requirements.

I have an input box where user can input the name. The value of input box gets saved into an array split by space.

Now I am copying array[0] to input box 1, array[1] to input box 2 ans so on.

<input id="txbox" type="text" value="Hello World" /> 

array[0]=Hello 
array[1]=World 

<input id="input1" type="text" value="Hello" style="display:none"/> 
<input id="input2" type="text" value="World"  style="display:none"/> 

As soon as the text gets copied, I want to hit/trigger ENTER inside the input area just after the text, without actually touching the keyboard (through JavaScript). How can I achieve that?

PS: the solution should work in IE8 and above and should be in plain JavaScript not jQuery.

Need of Simulating Enter:

I am using below code in my project. which converts input text from English to Hindi only on pressing enter just after the word. By this I can transliterate word by word.

    <html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script type="text/javascript" src="https://www.google.com/jsapi">
    </script>
    <script type="text/javascript">

      // Load the Google Transliterate API
      google.load("elements", "1", {
            packages: "transliteration"
          });

      function onLoad() {
        var options = {
            sourceLanguage:
                google.elements.transliteration.LanguageCode.ENGLISH,
            destinationLanguage:
                [google.elements.transliteration.LanguageCode.HINDI],
            shortcutKey: 'ctrl+g',
            transliterationEnabled: true
        };

        // Create an instance on TransliterationControl with the required
        // options.
        var control =
            new google.elements.transliteration.TransliterationControl(options);

        // Enable transliteration in the textbox with id
        // 'transliterateTextarea'.
        control.makeTransliteratable(['txtEnglish']);
      }
      google.setOnLoadCallback(onLoad);
    </script>
  </head>
  <body>
    Type in Hindi (Press Ctrl+g to toggle between English and Hindi)<br>
    <input type="text" id="txtEnglish" size="20"/>
    <input type="text id="txtHindi" size="20"  readonly/>
  </body>
</html> 

But, I have requirement like, user should enter the name in English and it should converts to Hindi and should display in other textbox which is readonly.

create a keypress event

here's my sample code in my app. Hope it helps.

Keycode 13 is the Enter key

 $('.search-input').keypress(function(event) {
          var search = $('.search-input').val();
          var keycode = (event.keyCode ? event.keyCode : event.which);
          if (keycode == '13') {
              var len = $('.search-input').val().length;

              if (len <= 0) {

                  $('#search-contents').slideDown();
                  $('.item-list').append('<div class="col-centered"><h3 class="title"> No Item/s Found.</h3></div>');
              } else {

                  $('#search-contents').slideDown();
                  $('html,body').animate({
                          scrollTop: $('#search-contents').offset().top
                      },
                      'slow');

                  searchFunction(search);


              }

          }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM