skramer
skramer

Reputation: 67

Simple Multiplication with jQuery

I can't seem to figure out out how to multiply 2 divs and display the total in another div. My several attempts were unsuccessful. For instance the end result would look like this:

<div class="itemOne">2</div>
<div class="itemTwo">5</div>
<div class="total">10</div>

Upvotes: 0

Views: 28637

Answers (3)

Adil
Adil

Reputation: 148110

Try this,

Live Demo

$('.total').text( parseFloat($('.itemOne').text()) * parseFloat($('.itemTwo').text()))

Upvotes: 1

j08691
j08691

Reputation: 207861

No need for jQuery here. In plain JavaScript try:

​var a = document.getElementsByClassName('itemOne')[0].innerHTML;
var b = document.getElementsByClassName('itemTwo')[0].innerHTML;
document.getElementsByClassName('total')[0].innerHTML = parseInt(a,10)*parseInt(b,10);​

jsFiddle example

Upvotes: 2

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26143

Try this...

var one = parseInt($(".itemOne").text(), 10);
var two = parseInt($(".itemTwo").text(), 10);
$(".total").text(one * two);

Upvotes: 6

Related Questions