Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,135 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,135 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!