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

Text input for Javascript

Hello,

I am trying to create a custom quote sheet. Is there any way possible with the code below to have the ability to identify the text input area in the script. I am asking this so that someone would have the ability to enter in the quantity of units manually and get a total number back.

So for example 20 units manually entered in the text area.

Then once the button is clicked, it would show the total. 20*5000.

Please see the code below.

Thanks for the help.

<!DOCTYPE html>
<html>
<body>


<textarea rows="1" cols="3">
20
</textarea>

<button onclick="myFunction()">Click Here For Total</button>

<p id="demo"></p>

<script>
function myFunction()
{
var x=20;
var y=5000;
x*=y;
var demoP=document.getElementById("demo")
demoP.innerHTML="Total=$" + x;
}
</script>

</body>
</html>

3 Answers

Kevin Korte
Kevin Korte
28,148 Points

Change var x to var x= document.getElementById('number').value;

'number' is the name of the id i gave the input box.

And a working prototype. I'd probably change your textarea into just an input.

http://codepen.io/kevink/pen/nEhGD

Kevin Korte thank you very much. Would you happen to know how or where I could find the ability to add commas to my final results?

Kevin Korte
Kevin Korte
28,148 Points

Not in particular tonight, but I found this: http://css-tricks.com/snippets/javascript/comma-values-in-numbers/

This might be a promising solution. I'll work on it tomorrow if I have time. In the meantime, you can work on it; but it's about time for bed for me!