Sibling checkbox condition check

This Meteor template event click .vote on the checkbox tries to check it's previous immediate sibling to see if it is checked. How can it be done? thx

Template.checkbox.events({
  'click .valid': () => {
     //do stuff
  },
  'click .vote': () => {  //99c
    //check if its nearest sibiling is checked else exit
    if ($(this).prevAll('input.valid').checked) {  //<------ not cutting it
       console.log('its sibling is checked');
       //do stuff
    } else {
      console.log('its sibling is not checked');
      //do other stuff
    }
  }
});
<template name="checkbox">
  <div class="checkbox-container">
    <div class="checkbox">
      <label class="check">
        <input class="valid" type="checkbox" name={{name}} value={{value}} checked={{checked}}>{{label}}
        <input class="vote" type="checkbox" name={{name}} value={{value}} checked={{checked}}>
      </label>
    </div>
  </div>
</template>

Answers 1

  • $(".vote").change(function() {
    
      if ($(this).closest('.check').find('input.valid').is(":checked")) { 
        console.log('its sibling is checked');
        //do stuff
      } else {
        console.log('its sibling is not checked');
        //do other stuff
      }
    }).change();
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="checkbox-container">
      <div class="checkbox">
        <label class="check">
            <input class="valid" type="checkbox" name={{name}} value={{value}} checked={{checked}}>{{label}}
            <input class="vote" type="checkbox" name={{name}} value={{value}} checked={{checked}}>
          </label>
      </div>
    </div>

    This is the solution you are looking :checked


Related Articles