mercredi 24 février 2016

JQuery checkbox and uncheck using if and else function - unable to get it working

Can one kindly help me. I'm very new to jQuery.

  • i have a checkbox with an id "#userj_neednotice"

what i want to do is:

  • when a user ticks the checkbox, the content with the class ".check_box_notice_content" should show
  • when the user unchecks the box, the content with the class ".check_box_notice_content" should hide
  • when the user has ticked the check box with the id "#userj_neednotice" and the page is refreshed and the checkbox is still ticked the content with the class ".check_box_notice_content" should show
  • i really do not want use toggle, your advise would be much appreciated

html

<div class="notice_info">
  <%= f.input :neednotice, label: "Do you need to give notice?", id: "userj_neednotice" %>
  <div class='check_box_notice_content hide'>
    <%= f.association :category_notice, collection: CategoryNotice.all, prompt: "please choose", label: 'notice period' %>
    <%= f.text_field :stratdate, label: "when can you start?", class: "datepicker", placeholder: "when can you start work? select date" %>                
  </div>
</div>

jQuery

$(document).ready(function() {
  $("#userj_neednotice").click(function () {
    $(".check_box_notice_content").removeClass("hide");
    if ($('.userj_neednotice').checked == true) {
      $(".check_box_notice_content").removeClass("hide");
    }
  });
});




Aucun commentaire:

Enregistrer un commentaire