greg.kujawa
6/11/2005 1:30:00 AM
This is a Rails-related question. I have several RHTML input forms
which have select drop-downs. My endusers complain that some of the
drop-downs are a bit lengthy to go through, since they can only type in
the first character of the select option and still have to scroll
through the choices under that.
Looking around the Internet I found some sample Javascript code that
will allow type-ahead so that each subsequent character of the select
option can be typed in order to narrow down the options.
Below is a snippet of the Javascript that accomplishes this:
----------------------
<script> w = ""; function keyp(e, controlList)
{ if (document.layers) { k = e.which; }
else{ k = window.event.keyCode; }
if (k==46) { if (w.length!=0) { w = w.substring(0,w.length-1); } }
else{ w += String.fromCharCode(k).toLowerCase(); }
for (x=0; x<controlList.options.length; x++)
{ z = controlList.options[x].text.substring(0,w.length).toLowerCase();
if (w==z) { controlList.options[x].selected=true; break; } } }
</script>
----------------------
I call this function by including the following code in my RHTML page:
----------------------
<form action="/employee/login" method="post" name="signIn">
Select the employee initials:<P>
<select NAME="employee[initials]" onkeydown="keyp(event, controlList);
return false;">
....
<script>
var
controlList=document.forms['signIn'].elements['employee[initials]'];
</script>
----------------------
This works fine, but those RHTML input forms where I have a select
drop-down defined with a Rails variable as part of the name breaks
things. For example:
----------------------
<form action="/employee/login" method="post" name="signIn">
Select the employee initials:<P>
<select NAME="<%= @employee %>[initials]" onkeydown="keyp(event,
controlList); return false;">
....
<script>
var controlList=document.forms['signIn'].elements['<%= @employee
%>[initials]'];
</script>
----------------------
Obviously the Javascript cannot parse what the Rails variable amounts
to. What would be a Ruby way of handling this to pitch the Javascript
altogether? Apologies if this is a bit off-topic. Figured there are a
lot of bright minds on this list and my bulb is a bit dim at the moment
:-)