INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Javascript to sum values

Javascript to sum values

(OP)
Hi,

I'm new to javascript & working on PHP where in I have 15 rows & multiple columns
The below function is to calculate the row wise multiplication

The screen will look something like this:
stock price value
1) 12 4 48
2) 11 5 55
.
.
15) 10 6 60
-----
Total 163

The value is getting populated by the below function.
What I need is how can i incorporate the total i.e. 163 within this function.
----------------------------------------
function multopst() {
var opst = ['vops1','vops2','vops3','vops4','vops5','vops6','vops7','vops8','vops9','vops10','vops11','vops12' ,'vops13','vops14','vops15'];
var lprc = ['vlpr1','vlpr2','vlpr3', 'vlpr4', 'vlpr5', 'vlpr6', 'vlpr7', 'vlpr8', 'vlpr9', 'vlpr10', 'vlpr11', 'vlpr12', 'vlpr13', 'vlpr14', 'vlpr15'];
var opval = ['vvops1','vvops2','vvops3','vvops4','vvops5','vvops6','vvops7','vvops8','vvops9','vvops10','vvops11','vvops12','vvops13','vvops14','vvops15'];

for (var index = 0; index < opst.length; index++)
{
var myBox1 = document.getElementById(opst[index]).value;
var myBox2 = document.getElementById(lprc[index]).value;
var result = document.getElementById(opval[index]);
var val01 = myBox1 * myBox2;
result.value = val01;
};
};
----------------------------------------------------------------
TIA,
Raj

RE: Javascript to sum values

Hi

I would try this first :

CODE --> JavaScript

function multopst()
{
    var opst = ['vops1','vops2','vops3','vops4','vops5','vops6','vops7','vops8','vops9','vops10','vops11','vops12' ,'vops13','vops14','vops15'];
    var lprc = ['vlpr1','vlpr2','vlpr3', 'vlpr4', 'vlpr5', 'vlpr6', 'vlpr7', 'vlpr8', 'vlpr9', 'vlpr10', 'vlpr11', 'vlpr12', 'vlpr13', 'vlpr14', 'vlpr15'];
    var opval = ['vvops1','vvops2','vvops3','vvops4','vvops5','vvops6','vvops7','vvops8','vvops9','vvops10','vvops11','vvops12','vvops13','vvops14','vvops15'];
    var total = 0;

    for (var index = 0; index < opst.length; index++)
    {
        var myBox1 = document.getElementById(opst[index]).value;
        var myBox2 = document.getElementById(lprc[index]).value;
        var result = document.getElementById(opval[index]);
        var val01 = myBox1 * myBox2;
        result.value = val01;
        total += val01;
    };

    document.getElementById('id_of_your_total').value = total;
}; 

If it not does what you expected, then you will have to provide more details of the task.

Feherke.
feherke.ga

RE: Javascript to sum values

(OP)
Thanks Feherke. It did work.

Except for "document.getElementById('id_of_your_total').value = total", I used $("#vtopsv1").html(topval);
because the field is in SPAN.

One more help I need to to calculate 2 fields & the result should come in the 3rd field

Sub total: 22
Discount: 11
Grand Total: 33

The sub-total comes from the function I had mentioned in my post & you helped me.
Discount field will be an input field where the user will be entering some value
Grand total has to come automatically thru a script.

I had worked on a script (given below) but showing no result. Is there a way to do so.
-------------------------------
function ttot() {

var opq1 = document.getElementById('vtopsq1').value;
var opq2 = document.getElementById('vtopsq2').value;
var opq3 = parseInt(opq1) + parseInt(opq2);
if (!isNaN(opq3)) {
$("#vtopsq3").html(opq3);
}
}
----------------------------------------

TIA,
Raj

RE: Javascript to sum values

Hi

Something is not clear. From your words I understood that you want to add a value coming either from a span tag or a variable and value from an input field. But in your code you are trying to get both values from input fields. So, what king of HTML elements are #vtopsq1 and #vtopsq2 ?

Feherke.
feherke.ga

RE: Javascript to sum values

(OP)
Thanks for quick response.

subtotal = vtopsq1 (data is populated from another function)
discount = vtopsq2 (input by the user)
grand total = vtopsq3 ( this will sum of the above 2 fields).

Below is HTML code for the above mentioned fields

<td align="right"><span class="style34" id="vtopsq1"></span></div></td>
<td><span class="style34"><input id ="vtopsq2" style="text-align: right;" size="6" name ="vtopsq2" maxlength="7"/></span></div></td>
<td align="right"><span class="style34" id ="vtopsq3"></span></div></td>

RE: Javascript to sum values

Hi

Well, if #vtopsq1 is a span element, it has no value attribute. The closest equivalent DOM property would be textContent :

CODE --> JavaScript

function ttot()
{
    var opq1 = document.getElementById('vtopsq1').textContent;
    var opq2 = document.getElementById('vtopsq2').value;
    var opq3 = parseInt(opq1) + parseInt(opq2);
    if (!isNaN(opq3)) {
        $("#vtopsq3").html(opq3);
    }
} 
Note that textContent is supported in Explorer only since version 9. As jQuery also supports Explorer only since version 9, its equivalent .text() method is similarly useless if you intend to support older Explorer too. In that case either use the innerHTML DOM property, or the same .html() jQuery method as you already used, but this time as getter ( without parameter ).

Feherke.
feherke.ga

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close