8

I am disabling a form based on a checkbox...

I am having trouble adding the disabled attribute.

here is what I got so far: HTML:

<table id="shipInfoTable">
  <tr>
   <td>Name:</td>
   <td><input type="text" name="name" /></td>
  </tr>
  ...
</table>

Javascript selector/attribute manipulation(jquery):

$("#shipInfoTable tbody tr td input").each(function(index, item){
    item.attr("disabled", true);
});

Chrome Dev Console error: Uncaught TypeError: Object #<an HTMLInputElement> has no method 'attr'

When I alert out the item within the .each() it alerts [object HTMLInputElement]

Not quite sure how to select the input element properly. What am I doing wrong?

Derek Adair
  • 21,846
  • 31
  • 97
  • 134

3 Answers3

12

The function will not give you a jQuery object. It should be:

$("#shipInfoTable input").each(function(index, item){
    $(item).attr("disabled", true);
});

(note that I also simplified your selector, it still works)
If you aren't doing anything eles with each item, this will work as well:

$("#shipInfoTable input").attr("disabled", true);
Kobi
  • 135,331
  • 41
  • 252
  • 292
  • 1
    Please note: in current versions of jQuery, this was changed to `.prop("disabled", true)`. I'm pretty sure `attr` didn't do was was intended anyway. – Kobi Feb 20 '13 at 10:52
4

....

$("#shipInfoTable tbody tr td input").each(function(){
    $(this).attr("disabled", true);
});
Pointy
  • 405,095
  • 59
  • 585
  • 614
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
0

the issue is the lack of $() in the .each() function...

$("#shipInfoTable tbody tr td input").each(function(index, item){
    $(item).attr("disabled", true);
});
Derek Adair
  • 21,846
  • 31
  • 97
  • 134