[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

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


Closed Thread
 
LinkBack Thread Tools
Old 08-15-2007, 01:17 PM   #1 (permalink)
SQLdumpster.com
 
miseria's Avatar
 
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 545
205.50 NP$ (Donate)

miseria will become famous soon enoughmiseria will become famous soon enough


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.

Thanks
__________________
!!!!!!! see something you like? Accepting offers for great affiliate websites !!!!!!!
Free Databases for your Website | All Things Playstation 3
Website Design Books | Music While You Work? | Computer Gadgets
miseria is offline  
Old 08-15-2007, 02:11 PM   #2 (permalink)
Stud Sausage
 
Join Date: Dec 2006
Location: England
Posts: 1,546
34.41 NP$ (Donate)

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);
__________________
My NamePros Tools
(firefox plugin, google gadget etc)
Matthew. is offline  
Old 08-15-2007, 02:20 PM   #3 (permalink)
SQLdumpster.com
 
miseria's Avatar
 
Join Date: Jun 2005
Location: West Sussex, UK
Posts: 545
205.50 NP$ (Donate)

miseria will become famous soon enoughmiseria will become famous soon enough


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.
__________________
!!!!!!! see something you like? Accepting offers for great affiliate websites !!!!!!!
Free Databases for your Website | All Things Playstation 3
Website Design Books | Music While You Work? | Computer Gadgets
miseria is offline  
Old 08-16-2007, 11:41 AM   #4 (permalink)
NamePros Member
 
Join Date: Sep 2005
Posts: 147
420.00 NP$ (Donate)

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:

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.
__________________
technfo.com - pick.pk - ajaxpro.es - gbt.es - jamones.cn - sqzd.com - geoinfo.net

Free URL Redirection (for sale) (62,000+ subdomains and growing!!) Real IP / Proxy IP

Last edited by guillermobt; 08-16-2007 at 11:45 AM. Reason: add a parseInt!
guillermobt is offline  
Closed Thread


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

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 04:10 PM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85