Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Cancel Bubble in select tag


Message #1 by gbrown@c... on Thu, 9 Jan 2003 13:15:06
for me it works fine ?!? (using IE6 sp1)
 <HTML>
 <HEAD>
<SCRIPT LANGUAGE=javascript>
function mefocus(objname)
{
 if(objname.cargo)
 {
  objname.cargo=""
 }
}
function mekey()
{
 o=event.srcElement
 cChar=String.fromCharCode(event.keyCode)
 o.cargo=o.cargo+cChar
 cargo=o.cargo
 cargo=cargo.toUpperCase()

 nLen=cargo.length
 lFound=false
 for(n=0;n<o.length;n++)
 {
  cChar=o.options[n].text
  cChar=cChar.toUpperCase()
  if(cChar.substring(0,nLen)==cargo)
  {
   if(lFound==false)
   {
    o.selectedIndex=n
   }
   lFound=true
  }
 }
 if(lFound==false)
 {
  o.selectedIndex=99999
 }
event.returnValue=false
event.cancelBubble=true
return false
}
</SCRIPT>
</HEAD>
<BODY onkeypress=mekey()>
<select cargo="" onfocus="mefocus(this)">
<option id=abcd>pair</option>
<option id=abcd>pine</option>
<option id=abcd>oppp</option>
<option id=abcd>pope</option>
<option id=abcd>poor</option>
</select>
</BODY>
</HTML>


<gbrown@c...> wrote in message news:250322@j...
>
> Hi
>
> I am trying to figure out how to override the default keypress behaviour
> in a select tag.
>
> I have a body onKeyDown event that works out what selectedIndex needs to
> be displayed.
>
> This javascript function receives a select object, cargo is a new text
> element within this object and contains what is being searched for.
>
> The Javascript routine correctly positions the list to the item being
> searched for but IE then takes over and moves to the wrong record.
>
> If the list contains pair, pine, pope, poor. Pressing P ends up on pine,
> pressing O initially displays pope (which is correct) then moves to the
> first record beginning with "O".
> Is it possible to stop this behaviour?
>
>
> function mefocus(objname)
> {
> if(objname.cargo){
> objname.cargo=""
> }
> }
> function mekey()
> {
> o=event.srcElement
> cChar=String.fromCharCode(event.keyCode)
> o.cargo=o.cargo+cChar
> cargo=o.cargo
> cargo=cargo.toUpperCase()
>
>
> nLen=cargo.length
> lFound=false
> for(n=0;n<=o.length;n++){
> cChar=o.options[n].text
> cChar=cChar.toUpperCase()
>
> if(cChar.substring(0,nLen)==cargo){
> if(lFound==false){
> o.selectedIndex=n
> alert(o.options[o.selectedIndex].text)
> }
> lFound=true
> }
> }
> if(lFound==false){o.selectedIndex=99999}
> event.returnValue=false
> event.cancelBubble=true
> return false
> }
>
>
>
> Regards
> Graham
>
>



  Return to Index