NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Javascript calculations

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 08-15-2007, 02:17 PM THREAD STARTER               #1 (permalink)
SQLdumpster.com
 
Encenta.com's Avatar
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 573
Encenta.com has a spectacular aura aboutEncenta.com has a spectacular aura about
 




Help! Javascript calculations


Hi, I'm looking for a way to calculate a set of dropdown lists. Eg:

HTML Code:
<select name="dd[0]">
<option value="+21">Value</option>
<option value="-21">Value</option>
</select>
Any help is appreciated.
????: NamePros.com http://www.namepros.com/programming/362229-javascript-calculations.html

Thanks
__________________
Encenta - Amazon Associates CMS
Encenta.com is offline  
Old 08-15-2007, 03:11 PM   #2 (permalink)
Senior Member
Join Date: Dec 2006
Location: England
Posts: 1,568
Matthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud of
 


Adoption Breast Cancer Breast Cancer Cancer Survivorship
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);
Matthew. is offline  
Old 08-15-2007, 03:20 PM THREAD STARTER               #3 (permalink)
SQLdumpster.com
 
Encenta.com's Avatar
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 573
Encenta.com has a spectacular aura aboutEncenta.com has a spectacular aura about
 




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.
__________________
Encenta - Amazon Associates CMS
Encenta.com is offline  
Old 08-16-2007, 12:41 PM   #4 (permalink)
NamePros Member
 
guillermobt's Avatar
Join Date: Sep 2005
Posts: 160
guillermobt is on a distinguished road
 



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:
????: NamePros.com http://www.namepros.com/showthread.php?t=362229

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 by guillermobt; 08-16-2007 at 12:45 PM. Reason: add a parseInt!
guillermobt is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 10:05 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger