1

I have two javascript text boxes.

<input type="text" name="test" value="300" />
<input type="text" name="test" value="500" />

How can I use javascript to alert the total price of the items in the text box?

Would something like this work?

var price = document.getElementById("test");
alert(price)

4 Answers 4

1

This will take all input elements into account (useful if you have many). Filter them by type and get their values in loop:

var inputs = document.getElementsByTagName("input");
var total = 0;

for (var i = 0; i < inputs.length; i++){
    if (inputs[i].type = "text"){
        total += parseInt(inputs[i].value, 10);
    }
}

alert(total);
Sign up to request clarification or add additional context in comments.

4 Comments

thank you it worked great. The only thing I had to change was document.getElementByTagName to document.getElementbyName. It worked either way, but I had other textboxes on the page that didn't have numbers in them. Thank you very much!
AFAIK getElementsByName does not work in IE, i'd prefer to use getElementsByTagName and filter by name inside loop
oh ok. do you know how i could also make it so that i could have a final text box named "priceList" and in that textbox, all the prices would be there from the array? Basically what it would do is take the two text box values and put them into one text box seperate by a comma
create input with needed id and assign desired value
0
<input id="test1" type="text" name="test" value="300" />
<input id="test2" type="text" name="test" value="500" />

JS:

var price = parseInt(document.getElementById("test1").value, 10) + parseInt(document.getElementById("test2").value, 10);

Comments

0
<input id="price1" type="text" name="test" value="300" />
<input id="price2" type="text" name="test" value="500" />

This will work:

var price = document.getElementById("price1").value+document.getElementById("price2").value;
alert(price)

Note Do not have other tags with id="price1" or id="price2"

Comments

0

What you have written will not work, for several reasons. Firstly, as the name suggests, getElementById gets an element based on the value of the id attribute. You haven't given your input elements an id attribute, so that's not going to work.

Secondly, document.getElementById('someId') returns an element, but you want the value. You can use the value property to get it:

var price1 = parseInt(document.getElementById("test1").value, 10);
var price2 = parseInt(document.getElementById("test2").value, 10);
alert(price1 + price2);

This will work with the following HTML:

<input type="text" name="test" id="test1" value="300" />
<input type="text" name="test" id="test2" value="500" />

Note the use of parseInt. The value property returns a String, so we use parseInt to attempt to parse that into a Number. If you don't use it, price1 + price2 will simply concatenate the strings.

2 Comments

well i would like to sum up the array. can i do getElementByName ? I will be extracting from a database so I do not know how many records there will be. All I know is that there will each have their own price in there. How can I make it so it sums up the array from the database?
Ahh sorry, I should have read your question more carefully. In that case, see @Igor Dymov's answer, which doesn't use getElementById and therefore means you won't have to change your HTML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.