I need to know about selecting multi check boxes in html using meteor.I did a sample.In this sample selecting more than one check box.how to get the selected data and how to store the data in array.Can you please see the below code and suggest me what to do?
The storage of the details in array as suppose arrayname = Bike,car,Computer(these are selected items)
HTML Code:
<form id="cb-form" action="action">
<input type="checkbox" name="owns" value="Bike">Bike<br/>
<input type="checkbox" name="owns" value="Car">car<br/>
<input type="checkbox" name="owns" value="Refridgerator">Refridgerator<br/>
<input type="checkbox" name="owns" value="Mobile">Mobile<br/>
<input type="checkbox" name="owns" value="Tablet">Tablet<br/>
<input type="checkbox" name="owns" value="Computer">Computer<br/>
<input type="submit" value="send" />
</form>
JS Code:
Template.login.events
({
'submit #cb-form' : function (e,t)
{
// template data, if any, is available in 'this'
if (typeof console !== 'undefined')
console.log("You pressed LOGIN Button");
e.preventDefault();
//retrieve the input field values
//here write get multiple check boxes data logic same as above scenario
}
});
HTML:
<template name="login">
<form id="cb-form" action="action">
<input type="checkbox" name="owns" value="Bike">Bike<br/>
<input type="checkbox" name="owns" value="Car">car<br/>
<input type="checkbox" name="owns" value="Refridgerator">Refridgerator<br/>
<input type="checkbox" name="owns" value="Mobile">Mobile<br/>
<input type="checkbox" name="owns" value="Tablet">Tablet<br/>
<input type="checkbox" name="owns" value="Computer">Computer<br/>
<input type="submit" value="send" />
</form>
</template>
JS:
Template.login.events({
'submit #cb-form' : function (event, template) {
event.preventDefault();
var selected = template.findAll( "input[type=checkbox]:checked");
var array = _.map(selected, function(item) {
return item.defaultValue;
});
console.log(array);
}
});
This outputs ["Car", "Refridgerator"] if Car and Refridge are selected. Its just easy use of underscore. Check underscore documentation for further reading.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With