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

HTML

Brian Patterson
Brian Patterson
19,588 Points

creating 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

statusHtml += '<li><input type="number" value="0" name="quantity_' + meal.item.replace(/\W/g, '_') + '"> name: ' + meal.item + ' price: '+ meal.price + '</li>';

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

It 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
Brian Patterson
19,588 Points

How would I do this Iain?