473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,406 developers and data experts.

Simple Form Suggestions

rnd me
427 Expert 256MB
Purpose: Allows you to create "presets" for text form inputs.

"Lightweight and simple to setup, it adds a lot of convenience for ~1kb of code."

Only one function, two parameters:

First argument is the element to upgrade.
you can specify the input's name, id or the input itself. you can also pass an array containing of any of these to easily bind multiple fields to the same suggestions.

Second argument is an array of suggestions.
it is a simple array of the text or numbers that will appear as suggestions.

This commented example has been tested in ie6+7,ff3,safariWin, opera9, and chrome.

see a live demo here


Expand|Select|Wrap|Line Numbers
  1.  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5.     <title>form suggestions demo</title>
  6.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. </head>
  8. <body onload="boot()">
  9. <h1>View Settings</h1>    
  10. <form>
  11.     Width    <input name='width' />    <br />
  12.     Height    <input name='height' />    <br />
  13.     Size        <input name='fontSize' />    <br />
  14.     Color        <input name='color' />    <br />
  15.             <input type='submit' />
  16. </form>    
  17.  
  18. <script type='text/javascript'>
  19.  
  20. if (!Array.prototype.map) { //  for IE compatibility...
  21.     Array.prototype.map=function(fun){
  22.     var L=this.length;var res=new Array(L);var thisp=arguments[1];
  23.     for(var i=0;i<L;i++){if(i in this){res[i]=fun.call(thisp,this[i],i,this);}}
  24.     return res;};
  25. }
  26.  
  27.  
  28. function bindSuggest(elm, ray) {
  29.  
  30. // first define a couple handy private inner functions:
  31.     function show2(s){if(s.split){s=el(s);}if(!s){return;}s.style.display="inline";}
  32.     function hide(s){if(s.split){s=el(s);}if(!s){return;}s.style.display="none";}
  33.  
  34. // start processing the arguments:
  35.     if (elm.join) { //handle arrays of tags/ids/names args
  36.       return elm.map(function (a) {bindSuggest(a, ray);});
  37.     }
  38.     if (elm.charAt) { //handle id/names args
  39.       elm = document.getElementById(elm) || 
  40.                document.getElementsByName(elm)[0] ;
  41.     }
  42.  
  43.  
  44. // if we can't find the input, we bail:
  45.     if(!elm){return 0;}
  46.  
  47. // create a new suggestion combo:
  48.     var s =document.createElement("select");
  49.     ray.map(function (a) {
  50.        var o=new Option(a); o.innerText=a; s.appendChild(o);
  51.     });
  52.  
  53. // a little style goes a long way:
  54.     var ss = s.style;
  55.     ss.display = "none"; ss.width="5em"; ss.padding="0.9em;" 
  56.     ss.textAlign="center"; ss.zIndex=555555;  ss.borderWidth="0px";
  57.  
  58. // bind the input's events:
  59.     elm.onfocus= function () { show2(s);}
  60.     elm.onblur= function () {  s.tim = setTimeout(function(){ hide(s);}, 150 ); };
  61.  
  62. // bind the combo's events:
  63.     s.onfocus= function () {  clearTimeout(s.tim);};
  64.     s.onblur= function () {  hide(s);};
  65.     s.onchange = function () {
  66.         elm.value = this.value;  elm.focus();
  67.         elm.select(); hide(s);
  68.     };
  69.  
  70. // tuck the combo next to the text input
  71.     elm.parentNode.insertBefore(s, elm.nextSibling);
  72.  
  73.   return s; // return the new combo
  74. }// end suggestion binder
  75.  
  76.  
  77.  
  78. // ###### customize the code below for your forms ######
  79.  
  80. // examples of how to use the function:
  81.  
  82. function boot(){ 
  83.  
  84. // bind several inputs to size presets:
  85.     bindSuggest( ["width","fontSize","height"] ,
  86.         [5,10,12,14,16,20,24,32,50,64,72,100,120,150,200] 
  87.     );
  88.  
  89. // bind just the color input to some colors:
  90.     bindSuggest( "color", "black,white,red,blue,green,gray".split(",") )
  91.  
  92. }//end boot
  93. </script>
  94. </body>
  95. </html>
  96.  
  97.  



here is a compressed version to tuck in your own scripts:

Expand|Select|Wrap|Line Numbers
  1. function bindSuggest(elm,ray){
  2. if (!Array.prototype.map) {Array.prototype.map=function(fun){
  3. var L=this.length;var res=new Array(L);var thisp=arguments[1];
  4. for(var i=0;i<L;i++){if(i in this){res[i]=fun.call(thisp,this[i],i,this);}}
  5. return res;};};
  6. function show2(s){if(s.split){s=el(s);}if(!s){return;}s.style.display="inline";};
  7. function hide(s){if(s.split){s=el(s);}if(!s){return;}s.style.display="none";}
  8. if(elm.join){return elm.map(function(a){bindSuggest(a,ray);});}if(elm.charAt){elm=document.getElementById(elm)||document.getElementsByName(elm)[0];}if(!elm){return 0;}var s=document.createElement("select");ray.map(function(a){var o=new Option(a);o.innerText=a;s.appendChild(o);});var ss=s.style;ss.display="none";ss.width="5em";ss.padding="0.9em;";ss.textAlign="center";ss.zIndex=555555;ss.borderWidth="0px";elm.onfocus=function(){show2(s);};elm.onblur=function(){s.tim=setTimeout(function(){hide(s);},150);};s.onfocus=function(){clearTimeout(s.tim);};s.onblur=function(){hide(s);};s.onchange=function(){elm.value=this.value;elm.focus();elm.select();hide(s);};elm.parentNode.insertBefore(s,elm.nextSibling);return s;}
  9.  
Dec 9 '08 #1
0 4458

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: r0adhog | last post by:
I have a very simple form: <html> <head> </head> <body> <% function ValForm() if len(document.form.newapp.all("AccessCode").Value) = 4 then document.form.newapp.submit()
3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
5
by: Digital Puer | last post by:
I have the following HTML form: - radio button A (default selected) - radio button B - input field, of type "file" with "Choose" button - submit button I would like to have it so that if the...
14
by: alwayshouston | last post by:
Hi All! I am working on this very small database and I am confused in the designing a simple form. I only have three tables in the database. First Table: tblExpense Columns: ExpenseID ;...
7
by: Steven | last post by:
Hello, First, let me state that I am trying to learn asp.net, so I am a beginner. Now on to the issue. I have a webform with a single Textbox and a FileSystemWatcher monitoring a directory for...
6
by: Brian | last post by:
Hello, I am using a beginners book on VB .net and already stumped....when using this code (below) , it will tell me that MsgBox is not valid. This is a very simple program and I can't figure out...
1
by: adam | last post by:
I have a simple form question. I order to access a payment gateway I have a asp.net page which has to have a form that use POST for method and _blank for target. Before the information of the form...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
5
by: Byron | last post by:
I need to create an application that uses primarily a single form rather than an SDI that creates a new form for everythting. However, I don't want an MDI style application since the users I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.