views:

350

answers:

2

I am using jQuery validate for client side validation and I want to ignore any element that has the style="display: none"

$("#myform").validate({
   ignore: "?"
});

What would my selector be for that in the above case?

+3  A: 

Use :hidden:

Elements can be considered hidden for several reasons:

  • They have a display value of none.
  • They are form elements with type="hidden".
  • Their width and height are explicitly set to 0.
  • An ancestor element is hidden, so the element is not shown on the page.
$("#myform").validate({
   ignore: ":hidden"
});

Update: for completeness, from the plugin's documentation:

ignore
Elements to ignore when validating, simply filtering them out. jQuery's not-method is used, therefore everything that is accepted by not() can be passed as this option. Inputs of type submit and reset are always ignored, so are disabled elements.

Felix Kling
@Felix: Is there a way to get validate() to return a boolean value?
gmcalab
@gmcalab: What should this value indicate? It returns a `Validator`: http://docs.jquery.com/Plugins/Validation#Validator
Felix Kling
@Felix thanks, nice link! Right now the form() is still not validate, its still validating the hidden fields....
gmcalab
So instead of ignoring the hidden elements, I disabled the hidden elements and it seems to work now.
gmcalab
@gmcalab: Good to hear that :) Don't know why it does not work with `:hidden`. As `.not()` takes any selector, this should work too.
Felix Kling
@Felix, yea I am not sure why it wont work with `:hidden`...how would you implement the `not()`
gmcalab
@gmcalab: I am not sure what you mean. `not()` is a function provided by jQuery.
Felix Kling
@Felix, I know that `not()` is provided by jQuery but I guess I thought you were giving an alternative to using `:hidden`, so I was asking for the implementation.
gmcalab
@Felix: I suppose :hidden is not working because the behaviour of :hidden changed in jQuery 1.4.2: non-visible elements that still consume space (visibility:hidden css property) aren't considered "hidden"
Franz
+1  A: 

http://api.jquery.com/hidden-selector/

":hidden"

is what your looking for.

Elements can be considered hidden for several reasons:

* They have a display value of none.
* They are form elements with type="hidden".
* Their width and height are explicitly set to 0.
* An ancestor element is hidden, so the element is not shown on the page.

I feel this might be better.

AjayP
Why would I want to ignore the visible fields?
gmcalab
Whoops, I was thinking to put a not() around it but I just saw hidden, changed.
AjayP