Jump to content

Javascript: totaling fields

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
Hey Codecallers, im working on a small website for my work, and ive got pretty much everything done except for the JS form with adding and subtracting values to some fields. Its a little complicated and hard to explain as how it works...

Lets say there are 3 "opportunities", by default, the value would be "0", which means 3 total opportunities, and 0 accomplished opportunities, if you change the value to 1, then that would mark it accomplished.., so if you mark 1 of the 3 as "1", (aka true, I guess), then it would be 3 total opportunities, and 1 accomplished opportunities... It also needs a value of "na", which has no value, but removes it as a possible opportunity.. Any idea how to make JS total this up?

Here is my best example of how it would work..

Defaults to..

Quote

Opportunity #1: 0
Opportunity #2: 0
Opportunity #3: 0
Total Opportunities: 3
Accomplished Opportunities: 0
As they accomplish opportunities, you alter the value from 0, which changes the total and accomplished.. such as

Quote

Opportunity #1: 1
Opportunity #2: 1
Opportunity #3: 0
Total Opportunities: 3
Accomplished Opportunities: 2
Now.. if an opportunity isnt accomplish-able, you change it to "N/A", which removes it from the total.

Quote

Opportunity #1: N/A
Opportunity #2: 1
Opportunity #3: 0
Total Opportunities: 2
Accomplished Opportunities: 1
Any idea how to make this in JS? the fields will be dropdown fields with "N/A", "0" and "1" as values.. Different tasks will have a different amount of opportunities, so the above examples have 3 opportunities, but it could go up to even like.. 20

Any help is appreciated!
Checkout my new forum! http://adminreference.com/

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Give each dropdownlist an onchange attribute to call 1 JS function.
Give all dropdownlists the same class.
Get them all doing Document.getElementsByClassName('classname');
loop trough all the elements gotten by this.
var total=0;

var accepted=0;

for (var i = 0; i < dropdownlists.length; i++) {

  if( dropdownlists[i].options[dropdown.selectedIndex].value == '1' ){

     accepted +=1;

  }

  if( dropdownlists[i].options[dropdown.selectedIndex].value != 'N/A'){

    total += 1;

  }

}

If you can't use the classname because other elements on the page must use the same, then give the dropdownlists 'good' IDs :)
like DDL1, DDL2, DDL3.

for (var i = 1; i <= amount; i++) {

  var DDL = Document.getElementById('DDL' + i);

  //do the same checks as in the above loop.

}.

You can normally fill the "amount" variable from the server side code, if required, by sending javascript/html code.

Edited by wim DC, 27 August 2010 - 01:30 AM.


#3
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
ok, any way to have 2 text fields (total opportunities, and accomplished opportunities), update automatically every time an opportunity is changed to 0, 1 or N/A ? Also, they may be set back to 0, so it needs to be able to go back and fourth
Checkout my new forum! http://adminreference.com/

#4
Guest_johnny.dacu_*

Guest_johnny.dacu_*
  • Guests
I haven't understood why you need the N/A value... It would be easy if you use checkboxes but is not an option anymore if you want a third value...