I am both intrigued and confused by the power of checkboxes. I want to use them to allow customers to select their preferred type of cuisine (fast food, Italian, sushi, etc.). Ultimately, I plan to match these preferences with restaurants offering those types of kitchens. I thought about storing all the choices in a 'kitchen' array[]. Is this the best approach, or should I save each choice as an individual variable? Here is a snippet of my HTML code:
<template name="hello">
<form class="main form page">
<div class="form-group">
<label class="control-label" </label>
<div class="checkbox">
<label> <input name="kitchen" type="checkbox" class="kitchen" id="italian"> Italian</label>
</div>
<div class="checkbox">
<label> <input name="kitchen" type="checkbox" class="kitchen" id="sushi"> Sushi </label>
</div>
<div class="checkbox">
<label><input name="kitchen" type="checkbox" class="kitchen" id="fastfood"> Fast Food </label>
</div>
</form>
</template>
The challenge lies in determining the most efficient way to store this data in the MongoDB database.
As I am using Meteor, I have to create a template event:
Template.hello.events({
'submit form': function(e) {
e.preventDefault();
Now, I need to save all the selected results to the database, where 'kitchens' will be the variable added to the Checkbox collection.
var kitchens = {
fastfood: $(e.target).find('[name=fastfood]').val(),
sushi: $(e.target).find('[name=sushi]').val(),
italian: $(e.target).find('[name=italian]').val(),
};
kitchens._id = Checkbox.insert(kitchens);
console.log("added to database");
The current problem is that I cannot determine if the checkbox was checked or not. I came across the following code to convert the check to either "Yes" or "No", but I'm unsure how to incorporate it into my existing code:
function kitchensChecked(id) {
var X = document.getElementById(id);
if (X.checked == true) {
X.value = "YES";
} else {
X.value = "NO";
};
Could someone assist me in integrating this code or suggest a better method for saving the checkbox results to the Checkbox collection?