Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select Multi Check Boxes in HTML using Meteor?

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
     }

  });
like image 422
Venkat Avatar asked Dec 25 '22 10:12

Venkat


1 Answers

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.

like image 62
chaosbohne Avatar answered Jan 12 '23 11:01

chaosbohne