Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Enter as tab //Javascript Function (See related posts)


Tabulation on fields through the enter key, i didnt allowed it to work on <select> and <textarea> since the enter is required to use this fields, so find a nice place to use it :]

Usage: just add the code on the end of your page or call it on the onload event (worse solution) and "the enter tabulation" will be added for all fields enclosed by the <form> tag.

[UPDATED CODE AND HELP CAN BE FOUND HERE]


@REQUIRES Event-Listener

   1  
   2  //Requires http://jsfromhell.com/geral/event-listener
   3  
   4  //+ Jonas Raoni Soares Silva
   5  //@ http://jsfromhell.com/forms/enter-as-tab [v1.0]
   6  
   7  enterAsTab = function(){
   8  	for( var f, i = ( f = document.forms ).length; i; addEventListener( f[--i], "keypress", function( evt ){
   9  		var el, l, k = evt.key == 13, e = evt.target;
  10  		if( k && !/textarea|select/i.test( e.type ) && !evt.preventDefault() ){
  11  			for( l = k = ( el = e.form.elements ).length; el[--k] != e; );
  12  			while( !(e = el[ k = ++k * ( k < l ) ]).type || e.disabled );
  13  			e.focus();
  14  		}
  15  	} ) );
  16  };



Example:

   1  
   2  <form action="">
   3  	<p>
   4  	<input type="text" />
   5  	<br /><input type="text" />
   6  	<br /> <input type="radio" name="aaa" /> opção 1
   7  	<input type="radio" name="aaa" checked="checked" /> opção 2
   8  	<br /><textarea rows="3" cols="15">aaaaaa pode dar enter q ele naum pula hahah</textarea>
   9  	<br /><select><option>opção blablablabla</option><option>opção bleblebleble</option></select>
  10  	<br /><input type="text" />
  11  	</p>
  12  </form>
  13  
  14  
  15  <script type="text/javascript">
  16  //<![CDATA[
  17  
  18  enterAsTab();
  19  
  20  //]]>
  21  </script>

You need to create an account or log in to post comments to this site.


Click here to browse all 5551 code snippets

Related Posts