/    Sign up×
Community /Pin to ProfileBookmark

radio; function; passing variables

I did a project a few years ago and was
quite successful thanks to webdeveloper.com
forum:

[URL=”http://www.epourania.com/wallcalc/wallcalc20.html”]wall calculator[/URL]

I’m now attempting a new project and using
the basic of this javascript code, however
I cannot seem to get it to pass variables
using the “… input type=”radio” …”.
I can get it to work with one radio, but
not with two.

[code=php]<html><head>
<title>generic calculator javascript</title>
<script type=”text/javascript” language=”JavaScript”>
<!–

function CalculateTotals()
{
f=document.calculator;
f.first_result.value = Number(f.first_number.value);
}

//–>
</script>
</head>

<BODY >
<form NAME=”calculator” method=”post” ACTION=””>
<TABLE>
<TR><TD>First Number</TD>
<TD><input type=”radio” NAME=”first_number” value=”1″
onclick=”CalculateTotals()”></TD></tr>

<tr><td>Second Number</td>
<td><input type=”radio” name=”first_number” value=”2″
onclick=”CalculateTotals()”></td></tr>

<TR><TD>First Result</td><td>
<input NAME=”first_result” size=”4″ >
</TD></TR>
</TABLE>
</form>
</body>
</html>[/code]

When I remove:

[code=php]<tr><td>Second Number</td>
<td><input type=”radio” name=”first_number” value=”2″
onclick=”CalculateTotals()”></td></tr>[/code]

it works fine.

The code I will be working on I developed in PHP:

[URL=”http://www.anothenservices.com/PHP/ranking/rank.php”]Currency Ranking Tool[/URL]

I’m hoping to do it in Javascript.

It takes two arrays and “… shift — unshift …” into a final array.

I want to avoid using a submit button and would rather
use Javascript because it’s faster and more efficient.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERNov 01.2010 — Try this instead ... ?
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;generic calculator javascript&lt;/title&gt;
&lt;script type="text/javascript"&gt;

function CalculateTotals(info) {
f=document.calculator;
f.first_result.value = Number(info);
}

&lt;/script&gt;
&lt;/head&gt;

&lt;BODY &gt;
&lt;form NAME="calculator" method="post" ACTION="" onsubmit="return false"&gt;
&lt;TABLE&gt;

&lt;TR&gt;
&lt;TD&gt;First Number&lt;/TD&gt;
&lt;TD&gt;&lt;input type="radio" NAME="first_number" value="1" onchange="CalculateTotals(this.value)"&gt;&lt;/TD&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Second Number&lt;/td&gt;
&lt;td&gt;&lt;input type="radio" name="first_number" value="2" onchange="CalculateTotals(this.value)"&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;TR&gt;
&lt;TD&gt; First Result&lt;/td&gt;
&lt;td&gt; &lt;input NAME="first_result" size="4" &gt; &lt;/TD&gt;
&lt;/TR&gt;

&lt;/TABLE&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@anothenauthorNov 01.2010 — YES!!

Now I need to spend the next hour trying to

figure out how you did that.

Thanks JMRKER
Copy linkTweet thisAlerts:
@JMRKERNov 01.2010 — You're most welcome.

Happy to help.

Good Luck!

Hints:

1. Biggest change was the 'onchange=' event rather than the 'onclick="

2. "this.value" contains the current select box (drop down) chosen.
Copy linkTweet thisAlerts:
@anothenauthorNov 01.2010 — You're most welcome.

Happy to help.

Good Luck!

Hints:

1. Biggest change was the 'onchange=' event rather than the 'onclick="

2. "this.value" contains the current select box (drop down) chosen.[/QUOTE]



Oh cool, I was looking over the function() and noticed "info"

I haven't used the function to return results before,

that sure makes things easier, and the " this.value "

from the form is associated with it.
×

Success!

Help @anothen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...