Archivi tag: enter

Javascript e catch del tasto “enter”

Ecco una semplice funzione Javascrip che consente di ottenere l’invio del contenuto di un campo di input semplicemente pigiando il tasto enter:

<scrip type="text/javascrip">
  function onEnter(evt, frm)
  {
      var keyCode = null;
   
    if( evt.which )
    {
         keyCode = evt.which;
     }
    else if( evt.keyCode )
    {
         keyCode = evt.keyCode;
     }
    
    if( 13 == keyCode )
    {
         document.searchForm.searchbutton.click();
         return false;
     }
   
     return true;
  }
  </scrip>

javascript11.jpg

Il codice HTML ad essa associato è il seguente:

<form id="searchForm" name="searchForm">
                <input id="input" name="input" type="text" value="Search..." onFocus="if (this.value == 'Search...') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'Search...';}" onKeyPress="return onEnter(event,this.form)"/>
                <input id="searchbutton" name="searchbutton" type="button" value="a" onClick="document.forms['searchForm'].submit()"/>
</form>

Alla prossima.