Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How do I convert my string to a number with jQuery/javascript?

So I'm utilizing the jquery ui w/ drag and drop tools. I have five columns and various objects that I drop into the columns. When said objects are dropped into a particular column they should add up and the sum is placed at the bottom of the column.

This is what I've attempted: $( ".section0" ).text(); parseInt($( ".section0" ).text());

Nothing seems to be working. It just keeps concatenating.

Link... https://w.trhou.se/u4zkoqjlwu

1 Answer

I think part of the issue you are experiencing is that you are trying to target the shapes based on a shared class which by completing the .text() function it is automatically concatenating the values of all matching class elements.

Noticed the results of

$(".section0") 

This gives an array when multiple shapes hold the same class.

Therefore maybe you can do something with a loop to cycle through each array entry to extract each value separately to calculate the total value. I will try and throw some working code together for you but food for thought for the time being.

The loop seems to have worked. Here's what I tried:

var f = 0; for (i=0; i < $( ".section0" ).length; i++) { f += parseInt($( ".section0" ).eq(i).text()); }; $( "#section0par" ).html(f);

Sorry for the unorganized code. I don't post a ton and markdown is not my friend.

Thanks for the help!