Forum Moderators: open

Message Too Old, No Replies

Pass a literal string inside of "document.forms."

Again, an incredibly simple one for you guys

         

Br3nn4n

1:58 am on Apr 3, 2010 (gmt 0)

10+ Year Member



Here's my code:

function $f(a,b) {return document.forms[a].+"income"+b+.value;}

So I can call it like $f(1,4) to get element named "income#" (# being the second argument) from form number 1.


But I'm getting an error. How do I do this correctly? Thanks!

astupidname

2:04 am on Apr 3, 2010 (gmt 0)

10+ Year Member



Try:

function $f(a,b) {return document.forms[a]["income"][ b ].value;}

Or maybe?:

function $f(a,b) {return document.forms[a]["income"+b].value;}

Br3nn4n

3:26 am on Apr 3, 2010 (gmt 0)

10+ Year Member



Tried the second one, the first one didn't seem like it would really work (no offense...haha). The second one works like a charm! Thanks! :)

astupidname

5:58 am on Apr 3, 2010 (gmt 0)

10+ Year Member



the first one didn't seem like it would really work (no offense...haha).


Sure, the first one will work, under the right conditions, was not entirely clear about your set-up there, but the first one would work with:

<script type="text/javascript">

function $f(a,b) {
return document.forms[a]["income"][ b ].value;
}

</script>
</head>
<body>
<div>
<form name="formZero">
1 <input type="radio" name="income" value="1"><br>
2 <input type="radio" name="income" value="2"><br>
3 <input type="radio" name="income" value="3"><br>
4 <input type="radio" name="income" value="4"><br>
5 <input type="radio" name="income" value="5"><br>
<input type="button" onclick="alert($f(0,3));" value="Try it">
</form>


And, you're welcome!