0

I have an external Javascript and I want to enable a button when the table is not empty.

But I cannot seem to make it work.

This is my code:

 $(function(){
    var rowCount = $('#uploadsTable tbody tr').length;

    if(rowCount != 0){
        document.getElementById("finalizeButton").disabled = false;
    }
});
6
  • "the able is not empty". What is "able"? Commented Mar 7, 2015 at 11:10
  • @dfsq, as per title it is table Commented Mar 7, 2015 at 11:11
  • Try this $("#finalizeButton").prop('disabled', false) instead of document.getElementById("finalizeButton").disabled = false Commented Mar 7, 2015 at 11:12
  • @bad_boy What is the difference? This is the same. Commented Mar 7, 2015 at 11:13
  • Does the table gets filled up or emptied out without reloading the page? Commented Mar 7, 2015 at 11:15

3 Answers 3

3

To disable button:

 $('#finalizeButton').attr('disabled','disabled');

So your code will be:

$(function(){
    var rowCount = $('#uploadsTable tbody tr').length;
    if(rowCount < 1){
        $('#finalizeButton').attr('disabled','disabled');
    } else {
        $('#finalizeButton').removeAttr('disabled');
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

This works! But I had to change the rowCount == 0. Meaning if it's empty, button is disabled, else it is enabled.
0

This should work. Of course, it has to be below the button, or in .ready():

$(function(){
    var rowCount = $('#uploadsTable tbody tr').length;

    if(rowCount == 0){
        document.getElementById("finalizeButton").disabled = true;
    }
});

Comments

0
please check following example:

<!DOCTYPE html>
<html>
<head>
<style>
table, td {
    border: 1px solid black;
}
</style>
</head>
<body>

<p>Click the button to return the number of tr elements in the table.</p>

<table id="myTable">
  <tr>
    <td>cell 1</td>
    <td>cell 2</td>
  </tr>
  <tr>
    <td>cell 3</td>
    <td>cell 4</td>
  </tr>
</table>
<br> 

<button onclick="myFunction()">Try it</button>
<button id = "buy"> Submit</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myTable").rows.length;
   if(x == 0){

      document.getElementById("buy").disabled = true;
    }else{

        document.getElementById("buy").disabled = false;
    }

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.