0

I have form with few fields and a dynamic table (ID- installments with "Add new Row" button. Entire form has submit button. I want to disable the submit button if the dynamic table has no row. And, if that table has a content (By click on Add New Row button) then submit button will be again active.

I have searched a lot and found below code :

<script>$(document).ready(function(){
var rowCount = $('#installments tbody tr').length;
if(rowCount < 1){
    $('#submit').attr('disabled','disabled');
} else {
    $('#submit').removeAttr('disabled');
}
});</script>

The submit button is disabled on page load (As there are no records found in dynamic table). I want to activate the submit button when records are added .

Please assist. Thanks in advance.

2
  • Please provide your HTML as well Commented Oct 19, 2019 at 20:32
  • All you need to do is run that function whenever a row is added or removed. You don't show your event handlers for that, but all that's needed is to call this same function inside those, after adding/removing the row. Commented Oct 19, 2019 at 20:32

2 Answers 2

1

Yes, you should call a function each time you add new row, also if you have a delete button you need to call the function to check if there are any rows left, if not - disable the submit.

Let's assume the id of your button for adding is "addNewRow" and id for deleting is "deleteRow" - then the code may look like this:

 <script>$(document).ready(function(){

    $("#addNewRow").click(function(){ 
                 /* your code for adding the row*/
                 buttonEnabler(); 
                 });
    $("#deleteRow").click(function(){ 
                 /* your code for removing the row*/
                 buttonEnabler(); 
                 });

    function buttonEnabler(){
    var rowCount = $('#installments tbody tr').length;
    if(rowCount < 1){
               $('#submit').attr('disabled','disabled');
        } else {
               $('#submit').removeAttr('disabled');
               }
    }

    });
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, Thanks for your reply. With this code, the submit button is enabled after clicking TWICE on AddNewRow button.On first click in AddNewRow the submit button is still disabled, after second click the submit button is getting enabled. Am I doing any mistake ?
Probably it is because you add new row after calling this function, you must execute code for adding, then call the functon.
I've updated my post to point the place where you should put the code responsible for adding or deleting
Thanks for your extended support. Yes the code was working as calling the function before adding the dynamic rows.
0

You can make function which handles the attr , call it on loading and on button click

$(document).ready(function(){
 function ButtonHandler(){
   var rowCount = $('#installments tbody tr').length;
   if(rowCount < 1){
      $('#submit').attr('disabled','disabled');
   } else {
      $('#submit').removeAttr('disabled');
   }
  }

ButtonHandler()

$('YourBtn').click(function(){
 ButtonHandler()
});

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.