Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically assigned var to control a form element? 1

Status
Not open for further replies.

blakekr

Programmer
Jul 19, 2000
11
0
0
US
Hi, all -- I program in perl but never warmed up to Javascript. Therefore, I'm stumped about:<br><br>How do I control a form element when I'm naming the element dynamically? My page has eleven separate, similar forms. One, for example, is Engineering. I want to tell Javascript to call function &quot;smartform('Engineering')&quot; and thereby control elements in form named Engineering.<br><br>Thus, I'd want smartform to control form elements like:<br><br>document.Engineering.resp_Engineering.checked<br>document.Engineering.resp_Engineering_accepted[1].checked<br>document.Engineering.resp_Engineering_approved[1].checked<br><br>For another form on the same page, of course, I'd want to call smartform() and control elements like:<br><br>document.Finance.resp_Finance.checked<br><br>Ways I have tried to accomplish this include:<br><br>1)<br>function smartform(origval) {<br>&nbsp;&nbsp;var val1 = &quot;resp_&quot; + origval + &quot;_accepted&quot;;<br>&nbsp;&nbsp;document.Engineering.val1[1].checked = true ...<br><br>... this fails because val1 is interpreted literally!<br><br><br>2)<br>function smartform(origval) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var pending = &quot;document.&quot; + origval + &quot;.resp_&quot; + origval + &quot;.checked&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;pending = false; ...<br><br>... this fails because false is literally assigned to the val pending, instead of resetting the form element.<br><br><br>Can anyone suggest a way to get a document.form.element statement to interpret a variable, instead of treating it like a string?<br><br>Any ideas are much appreciated!<br><br>
 
Dear blakekr,<br><br>Use the javascript eval() function like this<br><br>var formName = &quot;myform&quot;;<br>// create a 'string' that references the html form element<br>var elementRef = &quot;document.&quot; + formName + &quot;.theElement&quot;;<br>// get the actual object that the string references<br>var oElement = eval( elementRef);<br>// get the objects value ( assuming it has a .value entitity)<br>var elementValue = oElement.value;<br><br>alert(&quot;theElement is equal to &quot; + elementValue);<br><br>Hope this helps<br>-pete
 
Pete -- you're a savior! MUCH obliged!
 
Hey, guess what? I'm stuck again! My script works beautifully in MSIE now, and I only just noticed that Netscape rejects it. Does anyone know why? This is the header script:<br><br>====================================================<br>&lt;SCRIPT LANGUAGE=&quot;JavaScript&quot;&gt;<br><br>&lt;!--<br>function smartform(origval) {<br><br>var pendingtest = eval(origval + &quot;.resp_&quot; + origval);<br>var pending = eval(&quot;document.&quot; + origval + &quot;.resp_&quot; + origval);<br><br>var acceptedtest0 = eval(origval + &quot;.resp_&quot; + origval + &quot;_accepted[0]&quot;);<br>var acceptedtest1 = eval(origval + &quot;.resp_&quot; + origval + &quot;_accepted[1]&quot;);<br>var approvedtest0 = eval(origval + &quot;.resp_&quot; + origval + &quot;_approved[0]&quot;);<br>var approvedtest1 = eval(origval + &quot;.resp_&quot; + origval + &quot;_approved[1]&quot;);<br><br>var accepted0 = eval(&quot;document.&quot; + origval + &quot;.resp_&quot; + origval + &quot;_accepted[0]&quot;);<br>var accepted1 = eval(&quot;document.&quot; + origval + &quot;.resp_&quot; + origval + &quot;_accepted[1]&quot;);<br>var approved0 = eval(&quot;document.&quot; + origval + &quot;.resp_&quot; + origval + &quot;_approved[0]&quot;);<br>var approved1 = eval(&quot;document.&quot; + origval + &quot;.resp_&quot; + origval + &quot;_approved[1]&quot;);<br><br><br>if (approvedtest0.checked ¦¦ approvedtest1.checked ¦¦<br>&nbsp;&nbsp;&nbsp;&nbsp;acceptedtest0.checked ¦¦ acceptedtest1.checked) {<br>&nbsp;&nbsp;&nbsp;pending.checked = false;<br>}<br><br>if (approvedtest1.checked) {<br>&nbsp;&nbsp;accepted1.checked = true;<br>}<br><br>if (acceptedtest0.checked) {<br>&nbsp;&nbsp;approved0.checked = true;<br>}<br><br><br>}<br>// --&gt;<br><br>&lt;/SCRIPT&gt;<br>====================================================<br><br>... And this is a representative call to &quot;smartform&quot;:<br><br>====================================================<br>&lt;INPUT TYPE=RADIO CHECKED NAME=&quot;resp_Engineering&quot; VALUE=&quot;pending review&quot; onClick=&quot;smartform('Engineering')&quot;&gt;pending review<br>====================================================<br><br><br>... on MSIE, the whole thing runs fine, but Netscape contends that &quot;Engineering is undefined,&quot; and points to the first line of the smartform function, i.e.:<br><br>&gt; function smartform(origval) {<br><br>How can I convince it to run? Do I need to predeclare variables somehow in Netscape? Why does NS object so early in the process, before it even gets to the function?<br><br>Also, does anyone have a good book for Javascript? I have Danny Goodman's, and an old &quot;Programming Javascript for Netscape,&quot; and they've really been no help at all. I'm considering O'Reilly ...<br><br>Thanks for any insight!<br><br>
 
Hi, all -- in my bumbling way, I seem to have figured out the problem ... but I'm still interested in whether O'Reilly is considered the best JS reference book.<br><br>Thanks!<br>- blake
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top