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 trialBrian Patterson
19,588 Pointscreating a checkbox with an amount.
I am looking for a bit of guidance here. I am in the process of creating a till for a mock restaurant. Now this problem I'm having is that once someone selects an item how can you add a box to how many they want. Below is the code I have written.
$(document).ready(function (){
$.ajax({
type: 'GET',
url: '/data/coffee.json',
dataType:'json',
success: function(data) {
// // var data = JSON.parse(data)[0];
console.log('success', data);
let widget = show(data);
$("#Meals").html(widget);
}
});
});// end ready
function show(data) {
let statusHtml = '<ul>';
$.each(data[0].prices, function(i, meal){
statusHtml += '<li><input type="checkbox"> name: '+ meal.item + ' price: '+ meal.price +'</li>';
});
statusHtml += '</ul>';
return statusHtml;
}
So if I order a drink I want add how many I want of those drinks.
2 Answers
Iain Simmons
Treehouse Moderator 32,305 PointsstatusHtml += '<li><input type="number" value="0" name="quantity_' + meal.item.replace(/\W/g, '_') + '"> name: ' + meal.item + ' price: '+ meal.price + '</li>';
Iain Simmons
Treehouse Moderator 32,305 PointsIt may not look as nice, but you could just use text or number inputs for the quantity of each item instead of a checkbox. Default to zero for each one?
Brian Patterson
19,588 PointsHow would I do this Iain?
Iain Simmons
Treehouse Moderator 32,305 PointsIain Simmons
Treehouse Moderator 32,305 PointsOnly thing is, this number input is only for entering the quantity, you'd need to store and use the price per item and then have the code do a bunch of math to get the subtotals and totals.
Brian Patterson
19,588 PointsBrian Patterson
19,588 PointsThanks Ian for the reply.