0

I have this checkbox, how to disable a table (disable all the input fields in the table) whenever I check the checkbox?

<label><input type="checkbox" name="view" value="d">N/A</label> 

Is it possible using purely JavaScript and not jQuery.

This is the code now

var elems = document.getElementById('table-id').querySelectorAll('input,select,textarea');
document.getElementById('check').onchange = function () {
    if (document.getElementById('check').checked) {
        for (var i = 0; i < elems.length; i++) {
            elems[i].disabled = true;
        }
    } else {
        for (var i = 0; i < elems.length; i++) {
            elems[i].disabled = false;
        }    
        }
    }

and the table

<table id='table-id' border="1">

but it shows error like this:

Uncaught TypeError: Cannot read property 'querySelectorAll' of null

5
  • 3
    What do you mean when you say "disable a table"? Commented May 13, 2014 at 8:05
  • its input fields cannot be edited, written to Commented May 13, 2014 at 8:06
  • It is a table that has fields in it. text field, time, number, drop down and check boxes Commented May 13, 2014 at 8:07
  • 1
    So, you have a table containing a number of input fields, and you want to disable the inputs?? Commented May 13, 2014 at 8:08
  • @Matt Yes, precisely Commented May 13, 2014 at 8:10

2 Answers 2

5

Using jQuery, it is easy:

$("#tableId").find(":input").prop("disabled", true);

Using Javascript, it is a little longer:

var elems = document.getElementById('tableId').querySelectorAll('input,select,textarea');

for (var i = 0; i < elems.length; i++) {
    elems[i].disabled = true;
}

Fiddle: http://jsfiddle.net/abhitalks/6s7QL/2/

Update:

Added your checkbox code:

document.getElementById('checkboxId').onchange = function () {...
Sign up to request clarification or add additional context in comments.

9 Comments

@FrédéricHamidi: O yes, old habit of attr. Corrected.
And the OP want pure JS, not jQuery... (?)
Well, that's the problem of trying to be the Fastest Gun in the West ;) Retracted and reverted
@vaizaren: you are getting that error in the fiddle?
@vaizaren: please check the element id carefully.
|
2

Here is the pure JavaScript version to do the trick you want:

var table = document.getElementById('tableId'),
    inputs = table.getElementsByTagName('input'),
    checkbox = document.getElementById('checkboxId');

checkbox.onchange = function(e) {
    if(checkbox.checked) {
        disableTable(true);
    } else {
        disableTable(false);
    }
}

function disableTable(disableState) {
    for (var i = 0, l = inputs.length; i < l; i++) {
        inputs[i].disabled = disableState;
    }
}

And here is the working example in JSFiddle.

2 Comments

Uncaught TypeError: Cannot read property 'getElementsByTagName' of null js.html:43 (anonymous function)
The provided code in JSFiddle works fine. The error is specific to your customized code and the reason is that you don't have a table with tableId. Make sure to write the id of your table instead of tableId in the above code.

Your Answer

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