I want to make a function that will handle focus for multiple forms. I'm certainly doing something wrong here because it isn't working as expected. Can someone tell me what's wrong?
<a href="#" onclick="x('a','b','')">test</a>
<script type="text/javascript">
function x(xform, xbox, xval) {
document.xform.xbox.focus();
}
</script>
<input type="text" id="b" name="b" />
2 Answers 2
Not sure why you need the third value (focus does not take any parameter), but it should look like this:
document[xform][xbox].focus();
Explanation: document.xform will access the xform property of document. But document[xform] will access the property taken from the value of xform.
6 Comments
input element, you do element.value = newValue, so you could do document[xform][xbox].value = xvalYou can use bracket notation as Felix has...however IDs should be unique in a page, so just this would work:
function x(id) {
document.getElementById(id).focus();
}
Then in your code use the id that your <input> already has:
<a href="#" onclick="x('b')">test</a>
<form>? What do you mean by.focus('c')?focus('c')is a typo. I had alert in there and then tried focus. I'll edit my question. The form is below my input and is nameda