Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

meteor retrieve true/false value from checkbox switchChange

I have an event handler attached to checkboxes. I am using bootstrap switch http://www.bootstrap-switch.org/

I am trying to get the state value which is either true or false into a variable so I can set it to the session value.

I can get the true or false value when the state changes (as seen in the rendered code). However, I would like to get this value within events. Please check my x variable.

client.js

Template.myTemplate.rendered = function () {
 $('input[name="questions"]').bootstrapSwitch('state', true, true);
 $('input[name="questions"]').on('switchChange.bootstrapSwitch', function(event, state) {
 console.log(state); // true | false
});

html:

<template name="myTemplate">
 <div class="row">
  <input type="checkbox"  name="questions" unchecked> Hobby?
 </div>
 <div class="row">
  <input type="checkbox" name="questions" unchecked> Writer?
 </div>

I am trying to get the value which is printed out in console.log(state) within the rendered code into a variable within my event so I can set the session to value to either true or false.

Template.myTemplate.events({
'click input': function(event) {
  var x = $(this).is(":checked").val();
  Session.set("statevalue", x);
  console.log(Session.get("statevalue")); // this is not printing out anything
 }
});
like image 528
meteorBuzz Avatar asked Dec 29 '14 16:12

meteorBuzz


People also ask

How do you get the value from checkbox in Javascript is true or false?

1) You (un)selected the checkbox on the first page and submitted the form. 2) Your building the second form and you setting the value="" true/false depending on if the previous one was checked. 3) You want the checkbox to reflect if it was checked or not before.

How do you check if a checkbox is true or false?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.

How do I create a false value checkbox?

val( checkbox[0]. checked ? "true" : "false" ); This will set the value of the checkbox to "true" or "false" ( value property is a string) , depending whether it's unchecked or checked .


3 Answers

I think I have got a lot better sollution and not directly using JQuery:

Template.myTemplate.events({
'change input': function(event) {
  var x = event.target.checked;
  Session.set("statevalue", x);
  console.log(Session.get("statevalue"));
 }
});

Take care to see that I suggest you to use change instead of click in event.

like image 136
Paweł Smołka Avatar answered Nov 10 '22 17:11

Paweł Smołka


If you define id for your checkboxes, you could use

event.target.checkboxID.checked 

with in Template-event.

this will return true or false if checked or not.

like image 33
user3410842 Avatar answered Nov 10 '22 16:11

user3410842


You should use template instance:

Template.myTemplate.events({
'click input': function(event, template) {
  var x = template.$('input').is(":checked").val();
  Session.set("statevalue", x);
  console.log(Session.get("statevalue"));
 }
});

or maybe with 'target':

Template.myTemplate.events({
'click input': function(event) {
  var x = $(event.target).is(":checked").val();
  Session.set("statevalue", x);
  console.log(Session.get("statevalue"));
 }
});
like image 44
juliancwirko Avatar answered Nov 10 '22 18:11

juliancwirko