Skip to content

event.preventDefault(); is not working. #614

Description

@surenderrwt

Gemfile

gem 'cocoon'

_form.html.erb

<%= form_with(model: local_question, local: true) do |f| %>     
                      <div class="form-group">
                        <%= f.text_field :questiontitle, id: :local_question_questiontitle , class: "form-control input-custom input-full", placeholder: "Question Title" %>
                      </div>
                    
                      <div class="form-group">
                        <%= f.hidden_field :city_id, id: :local_question_city_id, value: :current_city %>
                      </div>
                    
                      <div class="form-group">
                        <%= f.hidden_field :business_user_account_id, id: :local_question_business_user_account_id, Value: current_business_user_account %>
                      </div>
                      <div id='question_options'>
                      <%=f.fields_for :question_options do |qo| %>
                        <%= render 'question_option_fields', f: qo %>
                      <% end %>
                      </div>
                      <%#= link_to_add_question_options "Add Option", f, :question_options %>
                      <div class='links'>
                        <%= link_to_add_association 'Add Option', f, :question_options, class: 'btn btn-primary', data: {association_insertion_node: '#question_options', association_insertion_method: :append } %>
                      </div>

                      <div class="form-group">
                      <br/>
                      <input 
                      type="text" 
                      class="form-control input-custom input-full" name="local_question[expiredate]" id="local_question_expiredate" placeholder="Select End Date" onfocus="(this.type='date')" onblur="(this.value == '' ? this.type='text' : this.type='date')" >   
                        <%#= form.datetime_select :expiredate, id: :local_question_expiredate, placeholder: "End Date" %>
                      </div>
                    
                      <div class="actions">
                        <%= f.submit "submit",  class:"btn btn-primary btn-lg" %>
                      </div>
                    <% end %>
<!-- end form-->              

_question_options_fields.html.erb

<div class='nested-fields'>
    <div class="form-group">
        <%= f.text_field :Optiontitle, placeholder: "Enter Options here..."  %>
        <%= link_to_remove_association "remove option", f, class: 'btn btn primary' %>
    </div>
</div>

JS is

  $('#question_options')
     .on('cocoon:before-remove', function(e, task) {
       if($('#question_options').find('input[type="text"]').length <= 4)
       {
           e.preventDefault(); // This is not working as expecting
       }
     });

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions