Javascript Focus Issues Solved

    
Posted on 02.11.10 at 2:35 pm by Gibby
I'm currently working on a very long form and want to make sure there are easy ways for the future users to navigate back and forth between elements without scrolling. I'm no javascript wizard, so I decided to just use the focus() function.

I ran into a problem though. Initially, I had something like this in my function

document.getElementById(field).focus();
where field was the id of the field I was trying to put the focus on. It worked sometimes, but for others it either didn't work or would submit the entire form! That was definitely not what I wanted it to do.

Doing a search for javascript help isn't always useful, so it took me a while to piece together what I needed to do. In the end, I needed to put the field in a variable local to the function then call the focus like this:

var field = document.getElementById(val);
field.focus();
Val is the id of the field. This worked like a charm. I have no idea why this works, but I figured I'd offer this little bit of information in case someone else ran into this problem. As I said, there wasn't much to go on by searching.


Comments (0)

Leave A Comment
 
 
 
 
 
Please type this word backwards: f28up