Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-05-2005, 08:16 PM   PM User | #1
Avalikia
New to the CF scene

 
Join Date: Mar 2005
Location: Utah
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Avalikia is an unknown quantity at this point
I need to set up a number conversion thing and don't know how.

I'm working on a role-playing game and need a way to let the members easily convert ages from human to the equivalent of the other races in the game and vice versa. I'm thinking of having something along the lines of a text box to put the number to be converted in, some way to select whether you're changing the number to or from human years, a list of races for you to choose from, a button for when you're done, and a text box that shows the result. I know how to set up the various form elements, but I don't know how to write the code that takes the selected options, does the math, and displays in the the result.

If someone can either explain how to do it or give me a link to a place that teaches you that sort of thing, I'd really appreciate it. It's high time I learned how to do this sort of thing!
Avalikia is offline   Reply With Quote
Old 03-05-2005, 11:45 PM   PM User | #2
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
Here's the basics, but it would help to have some age ratios to work with



<script language="javascript">
<!--

function convertme(){

input1=document["f1"]["t1"].value

for(i=0;i<document["f1"]["r1"].length;i++){
if(document["f1"]["r1"][i].checked){
race=document["f1"]["r1"][i].value
}
}

if(document["f1"]["r2"][0].checked){
convert=input1/race


document["f1"]["converted"].value=convert
}
else{

convert=input1*race
document["f1"]["converted"].value=convert
}

}

//-->
</script>

<form name="f1">
Age <input type="text" name="t1" value="1" size="3" maxlength="3"><BR>
Race 1<input type="radio" name="r1" value="5" checked> 5<BR>
Race 2<input type="radio" name="r1" value="7"> 7<BR>
Race 3<input type="radio" name="r1" value="9"> 9<BR>
Race 4<input type="radio" name="r1" value="15"> 15<BR>
Race 5<input type="radio" name="r1" value="20"> 20<BR>
Convertion <input type="text" name="converted"><BR><BR>

To Human <input type="radio" name="r2">
From Human <input type="radio" name="r2" checked>
<input type="button" value="convert" onclick="convertme()">
</form>


__________________
The silent one.

The most dangerous thing in the world is an idea.
The most dangerous person in the world is the one with an idea.

Last edited by Mr J; 03-06-2005 at 12:11 AM..
Mr J is offline   Reply With Quote
Old 03-08-2005, 05:14 AM   PM User | #3
Avalikia
New to the CF scene

 
Join Date: Mar 2005
Location: Utah
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Avalikia is an unknown quantity at this point
Oh, that's great just the way it is! I can see in the coding how to edit the ratios and labels for the radio buttons myself, which is good because I want to be able to add more races later when I need them (and I will).
Avalikia is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:47 PM.


Advertisement
Log in to turn off these ads.