troubleshooting Question

Object Expected on onkeypress

Avatar of JohnLucania
JohnLucania asked on
JavaScript
6 Comments1 Solution655 ViewsLast Modified:
<script>
var testString = "";
var timer = -1;
   
function addChar(selectBox, e) {
   try{
     
      clearTimeout(timer); // Prevents testString from being reset while the user types
     
      var selectedIndexOrig = selectBox.selectedIndex; // Preserve original index
     
      if (e.keyCode) {
         code = e.keyCode;
      } else if (e.which) {
         code = e.which;
      }
     
      if (code == 8) { // Backspace key pressed
         testString = testString.replace(/.$/, "");
      } else {
         testString += String.fromCharCode(code);
      }
     
      re = new RegExp("^" + testString, "i");
     
      var matched = false;
     
      var i;
      for (i = 0; i < selectBox.options.length; i++) {
         if (selectBox.options[i].text.match(re)) {
            setTimeout("document.getElementById('" + selectBox.id + "').selectedIndex = " + i, 5);
            timer = setTimeout("testString = ''", 1000); // Allow the user to try another word in 1 second
            matched = true;
            break;
         }
      }
     
      if (!matched) {
         setTimeout("document.getElementById('" + selectBox.id + "').selectedIndex = " + selectedIndexOrig, 5);
         timer = setTimeout("testString = ''", 1000); // Allow the user to try another word in 1 second
      }
   }catch(exception){ return; }
}
</script>

With this script, I put:

      <SELECT Name="#ATTRIBUTES.myFieldName#"
        Class="#ATTRIBUTES.myStyleClass#"
      tabindex="#ATTRIBUTES.myTabIndex#"
        onload="focus(this);"
      id="Titles" name="Titles"
        onkeypress="addChar(this, event);">

But, I am getting 'Object Expected' error on " onkeypress="addChar(this, event); ".
How do you correct that?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros