6 Replies - 1640 Views - Last Post: 08 October 2011 - 08:08 AM

#1 Archleone   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-October 11

question about running an average with javascript and select boxes

Posted 07 October 2011 - 12:41 PM

Hey there my task was to build a simple GPA calculator using select input and Javascript to average it all together and return it to the user with his name

so I started with my form which looks like this
<tr>
        <td>
            <form name="GPA"
            <p> Test 1</p>
        
        </td>
        <td>
<select id="v1">
<option value="4">A</option>
<option value="3">B</option>
<option value="2">C</option>
<option value="1">D</option>
<option value="0">F</option>
</select>
        </td>
    </tr>
        <tr>
        <td>


now to the best of my knowledge that looks correct, I made sure to use integers as the values so i could average them using this function

<script type="text/javascript">
<!--
function Average(v1,v2,v3)
{

return Average (v1+v2+v3) / 3;
}
//-->
</script>


Now im less sure about that but what I am really lost on (and I know for a lot of you this is easy, but I'm learning) is the button i would use to submit the form and run the java script. I would post mine but i'm kinda ashamed of it. any advice or directions to tutorials about this would help thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: question about running an average with javascript and select boxes

#2 Duckington   User is offline

  • D.I.C Addict

Reputation: 172
  • View blog
  • Posts: 615
  • Joined: 12-October 09

Re: question about running an average with javascript and select boxes

Posted 07 October 2011 - 01:13 PM

You need to close your form tag correctly. You have:

<form name="GPA"



It should be:

<form name="GPA">
// Other stuff here
</form>




With your average function, you don't need to call the same function inside itself, that would just cause problems. Simply:

function Average(v1,v2,v3)
{
return (v1+v2+v3) / 3;
}



Would work, though you need to make sure you are calling the function correctly.

If you're still having problems, show us your section of code where you are calling the function as well.
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4208
  • View blog
  • Posts: 13,283
  • Joined: 08-June 10

Re: question about running an average with javascript and select boxes

Posted 07 October 2011 - 02:29 PM

View PostArchleone, on 07 October 2011 - 09:41 PM, said:

Now im less sure about that but what I am really lost on (and I know for a lot of you this is easy, but I'm learning) is the button i would use to submit the form and run the java script.

you donít need to submit the form to run the script. actually, it would be easier not to submit at all (except when you need to do something with the data on the server side). the magic word is trigger. for that you can use a simple button.
<!-- form controls need a form -->
<!-- the action attribute is required! -->
<form action="">
    <!-- a label for the text box -->
    <label for="text_in">please enter some text</label>
    <!-- the text box itself -->
    <input type="text" id="text_in" name="mytext" value="some default value">
    <!-- something to trigger the Javascript -->
    <button type="button" id="trigger">show the value!</button>
</form>

// the executing function
// for simplicity just a simple alert
// per definition, the first parameter is the Event object
function showValue(evt)
{
    // do some accessor magic ;)/>
    alert(this.form.mytext.value);
}
// attach JS to the button
// works in every browser except IE 5-8
document.getElementById("trigger").addEventListener("click", showValue, true);

Was This Post Helpful? 1
  • +
  • -

#4 Archleone   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-October 11

Re: question about running an average with javascript and select boxes

Posted 07 October 2011 - 05:03 PM

The best I can do right now is an aproxomation of what I will use to call the function as im on my phone, but I want something like
/
<input type="button" name="submit" onclick= average(v1,v2,v3)/>



But im not sure if that's really what I need, then I need to get it to display it on another screen so ill look up the javascript to do that, thank you all very much for your help and knowledge I have a much better grasp of functions now
Was This Post Helpful? 0
  • +
  • -

#5 JMRKER   User is offline

  • D.I.C Addict
  • member icon

Reputation: 134
  • View blog
  • Posts: 868
  • Joined: 25-October 08

Re: question about running an average with javascript and select boxes

Posted 07 October 2011 - 06:39 PM

I'm a bit confused. :helpsmilie:

What are you trying to do when a selection is made?
Are the same 3 numbers averaged for each of the selections?
Where do the names for the grade assignment come in?
Was This Post Helpful? 0
  • +
  • -

#6 Archleone   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-October 11

Re: question about running an average with javascript and select boxes

Posted 07 October 2011 - 07:18 PM

I want to simply take the three letter grades convert them to a numerical number so they can be averaged and displayed by the script. At the bottem of these three drop down boxes I want a simple button that will run the script with the values input into the drop down boxes. I knew I had a problem somewhere because it wouldn't work I suspected it was in the button, but I wanted to confirm it so I posted what I had for verification, but not the button since that sounded like it would be against forum rules since it was so incomplete and part of an assignment we have
Was This Post Helpful? 0
  • +
  • -

#7 JMRKER   User is offline

  • D.I.C Addict
  • member icon

Reputation: 134
  • View blog
  • Posts: 868
  • Joined: 25-October 08

Re: question about running an average with javascript and select boxes

Posted 08 October 2011 - 08:08 AM

It is much easier to analyze the problem when all the parts are given.

Post what you have, incomplete or not. We can then decide how much help to give or correct what you have created thus far.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1