2

JS

I know this is a simple problem but i can't get answer for my form.I want to disable the submit button until the form is being saved.

 (function() {
    $('form > input').keyup(function() {
      alert("helloo");

        var empty = false;
        $('form > input').each(function() {
            if ($(this).val() == '') {
                empty = true;
            }
        });

        if (empty) {
            $('.save-form').attr('disabled', 'disabled');
        } else {
            $('.save-form').removeAttr('disabled');
        }
    });
}) ()

view

this is my form which contains various fields

- content_for :javascript_includes do
  = javascript_include_tag "dropdown.js", "business.js", "bootstrap-toggle.min.js"
.col-sm-12
  .main-login.main-center.customer_details
    = nested_form_for(@customer_detail) do |f|
      - all_views_side_error_messages!(@customer_detail)
      .form-alignment
        .fieldset
          .row
            .col-sm-12
            = f.label :customer_name,"Organisation/Customer Name", class: "col-sm-3 control-label text-right"
            = f.text_field :customer_name, autofocus: true, class: "col-sm-3"
            = f.label :residential_type,"Residential Type", class: "col-sm-3 control-label text-right"
            = f.check_box :residential_type, autofocus: true, "data-width" => "200", class: "toggle-two-resident select-resident col-sm-3"
        %br

        .fieldset
          .row
            .col-sm-12
            = f.label :customer_id,"Customer ID", class: "col-sm-3 control-label text-right"
            = f.text_field :customer_id, autofocus: true, class: "col-sm-3"
            = f.label :attention_to,"Attention To", class: "col-sm-3 control-label text-right"
            = f.text_field :attention_to, autofocus: true, class: "col-sm-3"
        %br

        .fieldset
          .row
            .col-sm-12
            = f.label :address,"Address", class: "col-sm-3 control-label text-right"
            = f.text_area :address, autofocus: true, class: "col-sm-3"
            = f.label :city,"City", class: "col-sm-3 control-label text-right"
            = f.text_field :city, autofocus: true, class: "col-sm-3"
        %br

        .fieldset
          .row
            .col-sm-12
            = f.label :pin_code,"Pin Code", class: "col-sm-3 control-label text-right"
            = f.text_field :pin_code, autofocus: true, class: "col-sm-3"
            %div{id: 'state_field'}
              = f.label :state,"State", class: "col-sm-3 control-label text-right"
              = f.select(:state, options_for_select(State.collect_state),{},{class: "selectpicker col-sm-3 column-width-change", prompt: "Select State", "data-live-search": "true"})
            %div.hide{id: 'country_field'}
              = f.label :country,"Country", class: "col-sm-3 control-label text-right"
              = f.select(:country, CountryList.collect_country_list,{}, {class: "selectpicker add_class_country dropdown_country col-sm-3 column-width-change", title: "select country", "data-live-search": "true"})

        %br

        .fieldset
          .row
            .col-sm-12
            = f.label :email,"Email", class: "col-sm-3 control-label text-right"
            = f.email_field :email, autofocus: true, class: "col-sm-3"
            = f.label :contact_no,"Contact No", class: "col-sm-3 control-label text-right"
            = f.text_field :contact_no, autofocus: true, class: "col-sm-3"
        %br

        .fieldset
          .row
            .col-sm-12
            = f.label :website,"Website", class: "col-sm-3 control-label text-right"
            = f.text_field :website, autofocus: true, class: "col-sm-3"
            %div.hide{id: 'currency_field'}
              = f.label :currency_type,"Currency Type", class: "col-sm-3 control-label text-right"
              = f.select(:currency_type, CurrencyType.collect_currency_type,{},{class: "selectpicker dropdown_business column-width-change", prompt: 'Select', "data-live-search": "true"})
            %div{id: 'change_resident'}
              = f.label :business_type,"Business Type", class: "col-sm-3 control-label text-right"
              = f.select(:business_type, BusinessType.collect_business_type,{},{class: "selectpicker dropdown_business column-width-change", prompt: 'Select', "data-live-search": "true"})
              %br
              %br

        .fieldset
          .row
            .col-sm-12
            = f.label :service_tax_reg_no,"Service tax Registration No", class: "col-sm-3 control-label text-right"
            = f.text_field :service_tax_reg_no, autofocus: true, class: "col-sm-3"
            = f.label :pan_no,"PAN No", class: "col-sm-3 control-label text-right"
            = f.text_field :pan_no, autofocus: true, class: "col-sm-3"
        %br

        .fieldset
          .row
            .col-sm-12
            = f.label :local_sales_tax_reg_no,"Local Sales Tax Registration No", class: "col-sm-3 control-label text-right"
            = f.text_field :local_sales_tax_reg_no, autofocus: true, class: "col-sm-3"
            = f.label :central_sales_tax_no,"Central Sales Tax Registration No", class: "col-sm-3 control-label text-right"
            = f.text_field :central_sales_tax_no, autofocus: true, class: "col-sm-3"
        %br

        = render partial: 'customer_details/goods_address', locals: {customer_detail: @customer_detail}
        %br

        .fieldset
          .row
            .col-sm-12
              = f.label :opening_bal,"Opening Balance if any?", class: "col-sm-3 control-label text-right"
              = f.text_field :opening_bal, autofocus: true, class: "col-sm-3"
              = f.label :business_center,"From which business center invoice is being raised?", class: "col-sm-3 control-label text-right business_center-alignment"
              = f.select(:business_center, BusinessType.collect_business_type,{},{class: "selectpicker dropdown_business column-width-change", prompt: 'Select', "data-live-search": "true"})
        %br


        .fieldset
          .row
            - can_download = params[:action] == 'edit' ? false : true
            .col-sm-3
              = f.submit "Save", class: "btn btn-primary save-form"
            .col-sm-3
              = f.submit "cancel", type: :reset, class: "btn btn-primary"
            .col-sm-3
              %div{id: 'download_form'}
                - if @customer_detail.save
                  = link_to "Download", download_csv_customer_detail_path(@customer_detail.id, format: "csv"), class: "btn btn-primary", disabled: can_download
            .col-sm-3
              = link_to("Print", "javascript:print()", class: "btn btn-primary")

can some one help me to solve this?.Thanks in advance

curiosity5678
  • 155
  • 1
  • 6
Hari Prakash
  • 425
  • 3
  • 15

3 Answers3

2

You have to use prop instead of attr:

if (empty) {
    $('.save-form').prop('disabled', true);
} else {
    $('.save-form').prop('disabled', false);
}

Or even shorter, no need for an if around, just use empty directly:

$('.save-form').prop('disabled', empty);

Working example.

eisbehr
  • 12,243
  • 7
  • 38
  • 63
  • See [this post](http://stackoverflow.com/questions/5874652/prop-vs-attr) on the differences between prop and attr @HariPrakash – Blue Aug 02 '16 at 07:46
  • okay.but the main problem is the button is not even disabling.and it doesn't come into JS first.can u plz check my JS code and tell me where i've missed? – Hari Prakash Aug 02 '16 at 07:48
  • With this change your code works fine, see: https://jsfiddle.net/qzrnd4oc/ @HariPrakash – eisbehr Aug 02 '16 at 07:53
  • Only thing you should do, is to `disable` the `button` at the beginning, otherwise the first check only runs when the first `input` was hit by `key`. I did it directl in the `html`, but you could trigger this with `js` at the start too. @HariPrakash – eisbehr Aug 02 '16 at 07:56
0

Try pure JS :

if (empty) {
   document.getElementsByClassName(".save-form")[0].setAttribute('disabled','disabled');
} else {
   document.getElementsByClassName(".save-form")[0].removeAttribute('disabled');
}
Sanzhar Dan
  • 351
  • 4
  • 11
0

Javascript

NodeList.prototype.addEventListener = function(e, handler) {
    for(var i = 0;i<this.length;i++)
        this[i].addEventListener(e, handler);
};
var inputs = document.getElementsByClassName("text-field");
inputs.addEventListener("keydown", function() {
    var noneEmpty = true;
    for(var input of inputs)
        if(input.value === "") 
            noneEmpty = false;
    document.getElementsByClassName("submit")[0].disabled = noneEmpty;
});

JQuery

$(".text-field").keydown(function() {
    var noneEmpty = true;
    $(".text-field").each(function() {
        if($(this.attr("value") === "") {
            noneEmpty = false;
            return false;
        }
    });
    $(".submit").attr("disabled", noneEmpty);
});

possible replacements:

.attr() by .prop()

.keydown() by .on("keydown", ...)

Error404
  • 719
  • 9
  • 30