NameSilo

Javascript calculations

SpaceshipSpaceship
Watch

SiKing

Registered MemberEstablished Member
Impact
6
Hi, I'm looking for a way to calculate a set of dropdown lists. Eg:

HTML:
<select name="dd[0]">
<option value="+21">Value</option>
<option value="-21">Value</option>
</select>

Any help is appreciated.

Thanks
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable Domains โ€” AI StorefrontUnstoppable Domains โ€” AI Storefront
Hi,

Can you elaborate a bit more please? You have the select there so I'm guessing you want to take the value of that and use it in a calculation but without more information I can't really provide much help.

As a tip, you can give the select an id and use it like this:
Code:
select = document.getElementById('selectid');
document.write(100 - select.value);
 
0
•••
Sorry, I was a bit rushed. Basically, I want to add the values of 5 dropdown boxes: dd[0], dd[1], dd[2], etc. and then display the calculated value in a div using innerHTML.
 
0
•••
Code:
<script language="javascript">
//GLOBAL Var.
var maxIndex= 5; //Put the # of form fields (takes in mind it starts numbering with the 0)


///////////////////////////////////////
function Sumatory (f, numFields) {
///////////////////////////////////////
  var sumatory= 0;
  for (k=0; k < numFields; k++) {
    theValue= f.dd[k].value;
    if (!(isNaN(theValue))) {
      sumatory+= parseInt(theValue);
    }
  }
  //return (sumatory);
  document.getElementById('visor').innerHTML= sumatory;
}

</script>

Add a DIV with ID="visor" somewhere in your code:

Code:
<div id="visor"></div>


Add a button in your form, something like this:

Code:
<input type="button" value="Add values"
  onClick="Sumatory (this.form, maxIndex)">


I personally would use Prototype library (www.prototypejs.org) to access DOM elements in a much powerful way. Once you use it, you'll love it.

Couldn't test the code, so I hope not to have made a syntax error or something like that.

Regards.
 
Last edited:
0
•••
Dynadot โ€” .com TransferDynadot โ€” .com Transfer
CatchedCatched
Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back