Need help with a function
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开发者_StackOverflow中文版="#" 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" />
Not sure why you need the third value (focus does not take any parameter), but it should look like this:
document[xform][xbox].focus();
DEMO
Explanation: document.xform
will access the xform
property of document
. But document[xform]
will access the property taken from the value of xform
.
You 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>
精彩评论