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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.