68

I have a row in a table which contains a checkbox and some other form fields (text boxes, hidden fields, select lists). When the checkbox is checked I want to disable all form fields in that row except for the hidden fields. I have this working for the most part, but I can't seem to ignore the hidden fields.

What is the best way to select all form fields in a table row but ignore hidden fields in the selection?

8 Answers 8

75
$(":input:not([type=hidden])")

":input" is a jQuery pseudo selector... "Selects all input, textarea, select and button elements." http://api.jquery.com/input-selector/

Sign up to request clarification or add additional context in comments.

Comments

47

Assuming by "hidden" you mean type="hidden" ie:

<input type="hidden" name="foo" value="bar">

then you can use the attribute not equals selector to do this:

$("tr input:checkbox").click(function() {
  var cb = $(this);
  var tr = $(this).closest("tr");
  if (cb.val()) {
    tr.find("input[type!='hidden']").attr("disabled", true);
  } else {
    tr.find("input[type!='hidden']").removeAttr("disabled");
  }
});

My general advice is avoid attribute selectors. They're slow. Give the relevant inputs (either the hidden ones or the not hidden ones) a class and then use that in the selectors.

If however you mean "hidden" as in "not visible" then use the :visible selector:

$("tr input:checkbox").click(function() {
  var cb = $(this);
  var tr = $(this).closest("tr");
  if (cb.val()) {
    tr.find("input:visible").attr("disabled", true);
  } else {
    tr.find("input:visible").removeAttr("disabled");
  }
});

4 Comments

@Cletus do you think that a filter() call with a function that checked whether "this.type" is "hidden" would be faster than the in-selector attribute test?
Ahhh, never even thought of tagging them with a class. Nice!
@cletus - Excellent tip on using classes. Just made my life worthwhile.
"input[type!=hidden]", i.e. without inner apostrophes, works too.
29

You can also solve this using the hidden selector

$("input:not(:hidden)")

See also: jQuery hidden selector

Comments

6

I found a simple way of doing this in jquery.

$("tr input").not("input[type='hidden']")

In code snippet below, there is a div with an id of tabsDiv that contains multiple html elements and this code gets all input elements inside this div except those with type of hidden.

$("#tabsDiv").find("input").not("input[type='hidden']")

Comments

1

.........

$('tr input').attr('disabled', true)
$('tr input[type="hidden"]').removeAttr('disabled')

Comments

0

First, you need to tag your "selector" checkbox (e.g. with a class attribute "selector) so that it is clear that it is used for checking instead of regular form element. Then you use this:

$('table :checkbox.selector').click(function(ev) {
    $(ev.currentTarget)
            .parents('td').siblings('td')
            .find(':input:not([type=hidden])')
            .attr('disabled', ev.currentTarget.checked);
});

This solution works for all inputs (eg. select lists, textareas) and it doesn't disable the selector checkbox itself.

I'm assuming you use latest jQuery.

Comments

0

Just add a filter of visible which means it will take only input element which are visible to users.
This worked for me I am adding description for 30 char.

$("input:visible")

Comments

0

You can use this, it worked fine for me:

var $inputs = $('#formArticle :input:not(:hidden)');

formArticle is my form name.

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.