The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Simple multiple row sum calculator, Halp : )
Osku
post Mar 15 2020, 01:29 AM
Post #1





Group: Members
Posts: 3
Joined: 3-March 20
Member No.: 27,217



Hello. I am new to all programming, and taking some basics course atm.

So here is the problem that I have now:

When the user click the button, it should print the sum of all the boxes.
I am not sure what I've done wrong with this code and a little help would be appreciated!


<!DOCTYPE html>
<html>
<meta charset="UTF-8"/>
<title>Ostorkori 🛒</title>
<body>
<h2>Ostoskori</h2>
Tuote 1: <input id="tuote_1"/>
<br>
Tuote 2: <input id="tuote_2"/>
<br>
Tuote 3: <input id="tuote_3"/>
<br>
Tuote 4: <input id="tuote_4"/>
<br>
Tuote 5: <input id="tuote_5"/>
<br>
Tuote 6: <input id="tuote_6"/>
<br>
<button type="button" onclick="hintaYht();">Yhteensä</button>
<br>
<div id="tulosX">
</div>

<script>
function hintaYht(){
var tuote1 = Number(document.getElementById(tuote_1).value);
var tuote2 = Number(document.getElementById(tuote_2).value);
var tuote3 = Number(document.getElementById(tuote_3).value);
var tuote4 = Number(document.getElementById(tuote_4).value);
var tuote5 = Number(document.getElementById(tuote_5).value);
var tuote6 = Number(document.getElementById(tuote_6).value);

var tulos = tuote1 + tuote2 + tuote3 + tuote4 + tuote5 + tuote6;
var x = document.createElement("tulos");
var t = document.createTextNode("This is a span element.");
x.appendChild(t);
document.body.appendChild(x);

document.getElementById("tulosX").innerHTML = tulos;

}
</script>



</body>
</html>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Mar 15 2020, 04:38 PM
Post #2


.
********

Group: WDG Moderators
Posts: 8,462
Joined: 10-August 06
Member No.: 7



QUOTE
document.getElementById(tuote_1)


The ID value must be quoted:
CODE
document.getElementById('tuote_1')

(unless it's a variable, but that's not the case here).
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 5th July 2020 - 12:57 PM