jquery: adding numeric values from to divs and displaying total

ar item_1 = $('#id_1').html()
var item_2 = $('#id_2').html()
var result = item_1 + item_2
ninjadude12Asked:
Who is Participating?
 
Gurvinder Pal SinghCommented:
try this
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
		<script src='jquery-1.4.2.min.js'></script>

		<script>
			$(document).ready(function(){
				$("#total").html( parseFloat( $('#price').html ( ).substring( 1 ) ) + parseFloat( $('#shipping').html( ).substring( 1 ) ) );
			});
		</script>
</HEAD>

<BODY>
	<div id="price">$5.00<div>
	<div id="shipping">$10.00<div>
    <div id="total">0.00<div>
</BODY>
</HTML>

Open in new window

0
 
ventaurCommented:
Instead of .html() use .text() to strip out tags and such. Of course, this only works if the text in those divs is really numeric.

var item_1 = $('#id_1').text();
var item_2 = $('#id_2').text();
var result = item_1 + item_2;

Open in new window

0
 
ninjadude12Author Commented:
can you have the total result display in for example #id_total and how
0
 
Gurvinder Pal SinghCommented:
$("#id_total").html( parseInt( $('#id_1').html ( ) ) + parseInt( $('#id_2').html( ) ) );
0
 
ninjadude12Author Commented:
tried it and it does not work



$(function(){

var item_1 = $('#price').text();
 var item_2 = $('#shipping').text();

 
  $("#total").html( parseInt( $('#price').html ( ) ) + parseInt( $('#shipping').html()
                                                               
                                                               
));

});

<div id="price">$5.00<div>
  <div id="shipping">$10.00<div>
    <div id="total">0.00<div>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.