Why can't I do this in a function?

function focus(element) {
    document.form.element.focus();
}

      

I want to have a function to focus on an input field, but I can't seem to do that. Is there a better way to do this?

+2


source to share


1 answer


If you are passing element

directly to a function, call focus

directly on that.

function focus(element) {
    element.focus();
}

      



If you are just passing the name of an element to a function, then ..

function focus(element) {
    document.form[element].focus();
}

      

+6


source







All Articles