I'm almost done with my quest to make a simulated Access listbox in hta by using javascript and a table. The code below works to have the table scroll to and highlight the first row where that letter shows up. In other words, if there's "Sam" and "Susan" in the column, it will always go to "Sam" when the 'S' key is pressed.
I've spent the better part of the day trying to find javascript code that counts the number of rows in the table that start with the "pressed" key, but I have had no luck. I want to do this so the user can keep pressing a certain key and scroll through all the rows that start with that letter (so hit "S" twice and go from Sam to Susan).
And if the letter doesn't exist in the table, I wouldn't want anything to happen (the same row that's currently highlighted stays highlighted if the letter isn't found).
I appreciate the help. Again, I have searched on tek-tips. Other than determining that a rows.length type of filter would need to be done, I don't know how to incorporate it.
Thanks again.
I've spent the better part of the day trying to find javascript code that counts the number of rows in the table that start with the "pressed" key, but I have had no luck. I want to do this so the user can keep pressing a certain key and scroll through all the rows that start with that letter (so hit "S" twice and go from Sam to Susan).
And if the letter doesn't exist in the table, I wouldn't want anything to happen (the same row that's currently highlighted stays highlighted if the letter isn't found).
I appreciate the help. Again, I have searched on tek-tips. Other than determining that a rows.length type of filter would need to be done, I don't know how to incorporate it.
Code:
function keystyped() {
if (event.keyCode > 64 && event.keyCode < 91) {
var rows = document.getElementsByTagName("tr");
for (var i = 0; i < rows.length; i++){
rows[i].style.backgroundColor = "";
rows[i].style.color = "black"; // <----------
}
var t=document.getElementById('mytable')
for (var i=1,l=t.rows.length;i<l;i++) {
if (t.rows[i].cells[0].innerHTML.substr(0,1)==String.fromCharCode(event.keyCode)) {
t.rows[i].scrollIntoView()
t.rows[i].style.backgroundColor = "#000066";
t.rows[i].style.color="white";
break
}
}
}
}
Thanks again.