Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select and disable each input field within a form, wrapped in a table in jquery

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?

like image 855
Derek Adair Avatar asked Feb 27 '10 18:02

Derek Adair


2 Answers

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);
like image 180
Kobi Avatar answered Sep 22 '22 06:09

Kobi


....

$("#shipInfoTable tbody tr td input").each(function(){
    $(this).attr("disabled", true);
});
like image 42
Sarfraz Avatar answered Sep 22 '22 06:09

Sarfraz