Rails Simple Form Bootstrap Javascript

Mel

I have a rails 4 app with bootstrap and simple form.

I have a simple form for datum.

It has a primary question, which if answered true, requires a follow up question. I want to hide the follow up question until the first question is answered as true.

I have defined a separate CSS class for the label above the follow up question (survey-link-data) so that the label is hidden and shown with the input field. This JS is incorrect - it doesn't hide at then show as I had hoped.

Please can you see what I've done wrong? Thank you.

These are my two simple form for (@datum) questions:

<%= f.input :survey, :as => :boolean, :label => false, inline_label: 'Do you need survey responses?'  %>
<br><br>


    <%= f.input :survey_link, label: 'Where is your survey?', :label_html => { :class => 'data-survey-link' }, placeholder: 'Include a link to your survey', :input_html => {:style=> 'width: 650px; margin-top: 20px',  class: 'response-project'} %>

This is my JS script:

<script>
$(function() {
    // Hide the survey_link input

    $('input[name="datum[survey_link]"]').hide();
    $('.data-survey-link').hide();
    // When the survey checkbox changes
    $('input[name="datum[survey]"]').change(function() {
         // If the survey checkbox is ticked
         if(this.checked) {
              // Survey checkbox was ticked - show the survey_link input
              $('input[name="datum[survey_link]"]')toggle(this.checked);
              $('.data-survey-link').show();

         } else {
              // Survey checkbox was unticked - hide the survey_link input
              $('input[name="datum[survey_link]"]').hide();
              $('.data-survey-link').hide();

         }
    });
});
</script>
Ernest

First of all, you are selecting, and hiding two elements (label and input), which is less efficient than just hiding/showing a wrapper (by default simple_form wraps input and label inside a special div tag). Let's give this wrapper a class:

<%= f.input :survey_link, wrapper_html: { class: 'survey-link-wrapper' } %>

I stripped the rest of the options for the simplicity of example.

You can initially hide it with CSS:

.survey-link-wrapper {
  display:  none;
}

Second, create a more efficient selector for first input:

<%= f.input :survey, input_html: { class: 'survey-checkbox' } %>

Then your javascript code becomes this:

<script>
$(function() {
    var $surveyLinkWrapper = $('.survey-link-wrapper');
    $('.survey-checkbox').change(function(e) {
         // If the survey checkbox is ticked
         if ( $(this).is(":checked") )  {
              // Survey checkbox was ticked - show the survey_link input
              $surveyLinkWrapper.show();
         } else {
              $surveyLinkWrapper.hide();
         }
    });
});
</script>

More clear and easier to read.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bootstrap markdown with Rails and Simple Form

From Dev

Simple Form Select with Rails Bootstrap

From Dev

bootstrap modal with Simple Form Rails

From Dev

Using bootstrap glyphicon in rails simple form attachment

From Dev

simple_form validations with Bootstrap in Rails

From Dev

Using bootstrap glyphicon in rails simple form attachment

From Dev

Rails 4, Bootstrap 3, simple_form - form styling not working

From Dev

Ruby on rails - bootstrap material design - Add checkboxes to simple form

From Dev

Custom wrapper for Bootstrap 3 dropdown in Rails simple form

From Dev

Rails4: How to apply Bootstrap to simple_nested_form_for

From Dev

bootstrap-wysihtml5-rails with simple_form

From Dev

Integrate Bootstrap 3 into simple_form using haml on Rails 4

From Dev

Simple Django Form with Bootstrap

From Dev

Rails Devise & Simple Form

From Dev

Rails with Simple Form - Styling

From Dev

Simple_form class form-horizontal with bootstrap 3 not working in rails 4

From Dev

Simple_form class form-horizontal with bootstrap 3 not working in rails 4

From Dev

How to create inline form for child records in horizontal main form with simple_form nested_form and bootstrap-sass Rails 4

From Dev

Rails - bootstrap_form_for

From Dev

Simple form not adding bootstrap classes

From Dev

simple Form validation with javascript

From Dev

Rails 4/Bootstrap 3: removing has-error wrappers from simple-form

From Dev

Closest div as container for bootstrap datepicker in rails and coffee script and simple_form

From Dev

Simple Form - Rails 4 - editing

From Dev

Rails Simple Form Country Select

From Dev

Foundation 6 with Rails Simple Form

From Dev

Rails Simple Form Association Not Found

From Dev

Simple Form not posting to DB Rails

From Dev

simple rails search form not working

Related Related

  1. 1

    Bootstrap markdown with Rails and Simple Form

  2. 2

    Simple Form Select with Rails Bootstrap

  3. 3

    bootstrap modal with Simple Form Rails

  4. 4

    Using bootstrap glyphicon in rails simple form attachment

  5. 5

    simple_form validations with Bootstrap in Rails

  6. 6

    Using bootstrap glyphicon in rails simple form attachment

  7. 7

    Rails 4, Bootstrap 3, simple_form - form styling not working

  8. 8

    Ruby on rails - bootstrap material design - Add checkboxes to simple form

  9. 9

    Custom wrapper for Bootstrap 3 dropdown in Rails simple form

  10. 10

    Rails4: How to apply Bootstrap to simple_nested_form_for

  11. 11

    bootstrap-wysihtml5-rails with simple_form

  12. 12

    Integrate Bootstrap 3 into simple_form using haml on Rails 4

  13. 13

    Simple Django Form with Bootstrap

  14. 14

    Rails Devise & Simple Form

  15. 15

    Rails with Simple Form - Styling

  16. 16

    Simple_form class form-horizontal with bootstrap 3 not working in rails 4

  17. 17

    Simple_form class form-horizontal with bootstrap 3 not working in rails 4

  18. 18

    How to create inline form for child records in horizontal main form with simple_form nested_form and bootstrap-sass Rails 4

  19. 19

    Rails - bootstrap_form_for

  20. 20

    Simple form not adding bootstrap classes

  21. 21

    simple Form validation with javascript

  22. 22

    Rails 4/Bootstrap 3: removing has-error wrappers from simple-form

  23. 23

    Closest div as container for bootstrap datepicker in rails and coffee script and simple_form

  24. 24

    Simple Form - Rails 4 - editing

  25. 25

    Rails Simple Form Country Select

  26. 26

    Foundation 6 with Rails Simple Form

  27. 27

    Rails Simple Form Association Not Found

  28. 28

    Simple Form not posting to DB Rails

  29. 29

    simple rails search form not working

HotTag

Archive