Javascript: How to check if check_box in rails is checked or not? [duplicate]

363
November 23, 2016, at 5:22 PM

This question already has an answer here:

  • show and hide div based on checkbox selection using javascript 1 answer

I have a check_box with id "current" in one of my views on my Rail application and I also have a text_field with id "end" in the same view. When the checkbox is clicked, I want the display of the text_field to be none.

I had a jQuery method which was not working:

$(document).ready(function() {
   if ($('#current').checked) {
      $("#end").show();
   }
   else {
      $("#end").hide();
   }
}
);

Anyway I can check the Boolean value of the check_box using Javascript?

Answer 1
$(document).ready(function() {
   if ($('#current').is(":checked")) {
      $("#end").show();
   }
   else {
      $("#end").hide();
   }
}
);

Try using :checked

Description: Matches all elements that are checked or selected.

READ ALSO
Bootstrap Validator & Image Picker - How to validate form if any one of two available select inputs are chosen?

Bootstrap Validator & Image Picker - How to validate form if any one of two available select inputs are chosen?

I am using bootstrap-validator for form validation, and imagePicker to display the selects. It works great, but I have come across an instance that I can't solve on my own.

369
Static files are missing

Static files are missing

In my app there is a /static folder which contains all the static resources needed by the app. Here is my handlers for the static resources:.

397
Form validity constantly returns true regardless of validator in the model

Form validity constantly returns true regardless of validator in the model

I'm using jQuery to hide and disable a submit button until a form has been validated with Paperclip's validates_attachment_file_name.

206