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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing variables to a function

Status
Not open for further replies.

waldorf

Programmer
Jul 5, 2000
4
US
This is my first time here, and I kinda feel like a sapling in the redwood forest, but maybe someone would stoop low enough to help me out.  I have successfully passed variables from a form input type="text" to a function, but when I try to pass input type="radio" to a function it returns 'undefined' and when I try to pass a <select> option, it returns 'null'.  Is there some way I can get my function to read these form types?
 
&lt;input type=&quot;radio&quot; name=&quot;ddd&quot; value=&quot;ddd&quot; onclick=&quot;alert(this.value)&quot;&gt;<br>This will alert the value associated with that radio button.<br><br>The select menus have an options collection.<br>&lt;select name=&quot;fff&quot; onChange=&quot;jive = this.selectedIndex;alert(this.options(jive).text)&quot;&gt;<br>&lt;option value=&quot;ddd&quot;&gt;ddd&lt;/option&gt;<br>&lt;option value=&quot;lll&quot;&gt;lll&lt;/option&gt;<br>&lt;/select&gt;<br><br>this code will alert lll or ddd accordingly. Just change the alerts to whatever function you need.<br><br><A HREF="mailto:-jared@aauser.com">-jared@aauser.com</A>
 
Thanks jaredn, I'm still studying your response.&nbsp;&nbsp;My problem may be a little different however, because it is a form where all the variables need to be compiled at the 'Submit' button.&nbsp;&nbsp;I should have put this up in the first message, here's an abbreviated program:<br><br>&lt;HTML&gt;<br>&lt;HEAD&gt;<br>&lt;SCRIPT LANGUAGE=&quot;JavaScript&quot;&gt;<br>function doit()<br>{&nbsp;&nbsp;<br>&nbsp;&nbsp;alert(&quot;hello &quot; + document.myform.title.value + &quot; &quot; + document.myform.fname.value + &quot; &quot; + document.myform.lname.value + &quot; from &quot; + document.myform.state.value)<br>}<br>&lt;/SCRIPT&gt;<br>&lt;/HEAD&gt;<br>&lt;BODY&gt;<br>&lt;FORM NAME=&quot;myform&quot;&gt;<br>&nbsp;&nbsp;What is your First Name? <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;text&quot; name=&quot;fname&quot;&gt;&lt;p&gt;<br>&nbsp;&nbsp;What is your Last Name? <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;text&quot; name=&quot;lname&quot;&gt;&lt;p&gt;<br>&nbsp;&nbsp;What is your title?<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;radio&quot; name=&quot;title&quot; value=&quot;Mr.&quot;&gt;Mr.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;radio&quot; name=&quot;title&quot; value=&quot;Mrs.&quot;&gt;Mrs.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;radio&quot; name=&quot;title&quot; value=&quot;Miss&quot;&gt;Miss&lt;p&gt;<br>&nbsp;&nbsp;What state are you from?<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;select name=&quot;state&quot; size=1&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=&quot;California&quot;&gt;CA<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=&quot;Nevada&quot;&gt;NV<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=&quot;Arizona&quot;&gt;AZ<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value=&quot;47/otherstate&quot;&gt;Everywhere Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/select&gt;&lt;p&gt;<br>&nbsp;&nbsp;&lt;input type=&quot;button&quot; value=&quot;Submit&quot; onClick=&quot;doit()&quot;&gt;<br>&lt;/FORM&gt;<br>&lt;/BODY&gt;<br>&lt;/HTML&gt;<br><br>The values of fname and lname are passed successfully, but not title or state, so the final result says &quot;hello undefined Ronald McDonald from null&quot;&nbsp;&nbsp;I can't just put the function call in the middle of a form.&nbsp;&nbsp;Can you see my dilema?&nbsp;&nbsp;How can I fix this?
 
Dear Waldorf,<br><br>In client script you are running under the Browser. Your three input radio elements make a collection under the form object. So you need to look at each ones 'checked' property to determine which one is currently checked.<br><br>I don't know why you can't see the &quot;state&quot; element unless you are running under Nutscrap. Nutscrap does not support the 'select' object's .value property. Instead you must get the value from the currently selected index of the 'options' collection of the select element.<br><br>But in IE this works:<br><br>function doit()<br>{&nbsp;&nbsp;<br>&nbsp;&nbsp;var oTitles = document.myform(&quot;title&quot;);<br>&nbsp;&nbsp;var sTitle = new String(&quot;none&quot;);<br>&nbsp;&nbsp;for(i=0; i&lt; oTitles.length; i++){<br><br>&nbsp;&nbsp;&nbsp;&nbsp;if ( oTitles<i>.checked){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sTitle = oTitles<i>.value;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;alert(&quot;hello &quot; + sTitle + &quot; &quot; + <br>&nbsp;&nbsp;&nbsp;&nbsp;document.myform.fname.value + &quot; &quot; + <br>&nbsp;&nbsp;&nbsp;&nbsp;document.myform.lname.value + &quot; from &quot; <br>&nbsp;&nbsp;&nbsp;&nbsp;+ document.myform.state.value)<br>}<br><br>Good luck<br>-pete<br>
 
Seeing as how nobody has given me a link to a good DOM description (hint hint *smile* -- how did all of you guys get proficient with the DOM?), I can't tell you exactly how it goes.. but I know that I've worked with code that properly returned the value from any form element in both browsers. Not all of them go by the name of &quot;value&quot;, though, I remember that. So let the carrot be that &quot;it can be done&quot;, and best of luck. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
This is interesting.&nbsp;&nbsp;&nbsp;Thanks palbano, I'll give your suggestion a try.&nbsp;&nbsp;I was hoping, however, to get both types to pass in both browsers, (obviously) and imotic (nice website, btw) you've given me hope, I just need the right person to look at this.&nbsp;&nbsp;C'mon, I know you're out there somewhere.&nbsp;&nbsp;Anyone know THE answer to this?&nbsp;&nbsp;(kinda makes me wish I knew DOM.)
 
Dear Waldorf,<br><br>This works in Nutscrap 4.72 and IE 5, that's all I have on this machine to test with.<br><br>Now I took the time to go out on the internet and find the minor changes to make my previous code work in Nutscrap. The real question is, why wouldn't you do it?<br><br>function doit()<br>{&nbsp;&nbsp;<br> <br>&nbsp;&nbsp;var oTitles = document.myform.title;<br>&nbsp;&nbsp;var sTitle = new String(&quot;none&quot;);<br>&nbsp;&nbsp;for(i=0; i&lt; oTitles.length; i++){<br><br>&nbsp;&nbsp;&nbsp;&nbsp;if ( oTitles<i>.checked){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sTitle = oTitles<i>.value;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;sState = document.myform.state.options[document.myform.state.selectedIndex].value;<br><br>&nbsp;&nbsp;alert(&quot;hello &quot; + sTitle + &quot; &quot; + <br>&nbsp;&nbsp;&nbsp;&nbsp;document.myform.fname.value + &quot; &quot; + <br>&nbsp;&nbsp;&nbsp;&nbsp;document.myform.lname.value + &quot; from &quot; <br>&nbsp;&nbsp;&nbsp;&nbsp;+ sState);<br>}<br><br>If you keep making me do your work your going to have to start sending me some of your paycheck!<br><br>-pete
 
Thanks Pete, I have been out looking also, finding soultions myself, posted this on a couple different forums, and I got exactly what I have been looking for - more than one answer.&nbsp;&nbsp;Thanks for all the blood sweat and tears you had to go through to find this.&nbsp;&nbsp;I am a government worker, and if I gave you part of my paycheck, you wouldn't have to wonder why the government isn't run any better!&nbsp;&nbsp;Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top