Javascript: Select dropdown menu onChange and Key Events

  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 437

Post 3+ Months Ago

I added a small javascript function to my site that I call in an onChange handler on a <select> dropdown.

Code: [ Select ]
function dropdown_change(id){
             for (i = 0; i < document.form1.elements.length; i++) {
                    if (document.form1.elements[i].tabIndex == id.tabIndex+1) {
                    document.form1.elements[i].focus();
                    }
            }
}

<form name='form1'><select tabindex="1" name="salutation" id="sal" onchange="dropdown_change(this);"><option....

<input type="text" name="nextfield" tabindex="2">
  1. function dropdown_change(id){
  2.              for (i = 0; i < document.form1.elements.length; i++) {
  3.                     if (document.form1.elements[i].tabIndex == id.tabIndex+1) {
  4.                     document.form1.elements[i].focus();
  5.                     }
  6.             }
  7. }
  8. <form name='form1'><select tabindex="1" name="salutation" id="sal" onchange="dropdown_change(this);"><option....
  9. <input type="text" name="nextfield" tabindex="2">


As long as my tabindexes are accurate and complete the function fires properly and when the salutation is changed via mouseclick then the focus will jump to the next tabindex. My problem is with IE. I've found that due to a bug that IE fires the onchange when a button event is triggered. In Chrome this works as it should. I need it to ... if the user uses the mouse and selects and item that it jumps to the next element else if the user decides to use the keyboard buttons up and down (KeyCodes 38 and 40) that they can change the select dropdown with them. I've tried to wrap the whole FOR loop in an IF/ELSE statement that maybe says:

Code: [ Select ]

function(){

if keyCodePressed == 40 or 38
do nothing
else
run the for loop, get next tabID and set focus.

}
  1. function(){
  2. if keyCodePressed == 40 or 38
  3. do nothing
  4. else
  5. run the for loop, get next tabID and set focus.
  6. }



Thanks for any help on showing me how to combine the two events.

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: thanhpatin and 111 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.