Mitch Evans
Mitch Evans

Reputation: 641

Multiplying two cells in a table together

jsFiddle: http://jsfiddle.net/KSCLC/3/

I have a script (below) that adds a row to a table.

I am wanting to add two cells together to get a total

Item Price * Item Weight = Line Total 

if (field.value.length != 0) {
    var upc=document.getElementById("UPC").value;
    var weight=document.getElementById("weight").value +"lbs";
    var table=document.getElementById("ScannedItems");
    var row=table.insertRow(-1);
    var cell1=row.insertCell(0);
    var cell2=row.insertCell(1);
    var cell3=row.insertCell(2);
    var cell4=row.insertCell(3);
    var cell5=row.insertCell(4);
    var cell6=row.insertCell(5);
    cell1.innerHTML=upc;
    cell2.innerHTML="Example Description";
    cell3.innerHTML="$3.00";
    cell4.innerHTML=weight;
    var total=cell3.innterHTML*cell4.innerHTML;
    cell5.innerHTML="$" +total;
    cell6.innerHTML="<a><span class='glyphicon glyphicon-plus' style='padding-right:15px;'></span></a><span>&nbsp;</span><a><span class='glyphicon glyphicon-minus'></span></a>";
    field.value ='';
}

I have tried making a new variable after the two cells and before the new cell but all I get is $Nan in the table.

The specific lines I am messing with is:

cell3.innerHTML="$3.00";
cell4.innerHTML=weight;
var total=cell3.innterHTML*cell4.innerHTML;
cell5.innerHTML="$" +total;

Upvotes: 0

Views: 620

Answers (2)

Jakub Kotrs
Jakub Kotrs

Reputation: 6244

First, you need to parse integers out of user input. Next, you need to multiply. And THEN append any units:

var weight = parseInt(document.getElementById("weight").value);
var price = 3;
var total = weight * price;

//

cell3.innerHTML = "$" + price.toFixed(2);
cell4.innerHTML = weight + "lbs";
cell5.innerHTML = "$" + total.toFixed(2);

Edit based on comments

Accessing data-attributes is done via getAttribute and setAttribute.

cell5.setAttribute('data-total', total); // caches the total into data

var total = cell5.getAttribute('data-total'); // reads the value

Upvotes: 2

Shafeeque
Shafeeque

Reputation: 2069

I am just pointing the errors, You can debug

    var upc=document.getElementById("UPC").value;
    var weight=document.getElementById("weight").value +"lbs"; // here appending lbs to weight
    var table=document.getElementById("ScannedItems");
    var row=table.insertRow(-1);
    var cell1=row.insertCell(0);
    var cell2=row.insertCell(1);
    var cell3=row.insertCell(2);
    var cell4=row.insertCell(3);
    var cell5=row.insertCell(4);
    var cell6=row.insertCell(5);
    cell1.innerHTML=upc;
    cell2.innerHTML="Example Description";
    cell3.innerHTML="$3.00"; // value having $, so maths operation won't work
    cell4.innerHTML=weight; // value having lbs, so maths operation won't work 
    var total=cell3.innterHTML*cell4.innerHTML; // Is not an integer 

Instead of above you can try something like this

   var weight=document.getElementById("weight").value;
   var table=document.getElementById("ScannedItems");
   var row=table.insertRow(-1);
   var cell1=row.insertCell(0);
   var cell2=row.insertCell(1);
   var cell3=row.insertCell(2);
   var cell4=row.insertCell(3);
   var cell5=row.insertCell(4);
   var cell6=row.insertCell(5);
   cell1.innerHTML=upc;
   cell2.innerHTML="Example Description";
   cell3.innerHTML="$3.00";
   cell4.innerHTML=weight+"lbs";
   var total=3*weight;

Upvotes: 0

Related Questions