Alex F
Alex F

Reputation: 27

jQuery :not() Selector doesn't work

I want to exclude selects with empty values from serialization.

I'm trying to achieve this like so:

    var form = $('#myForm');
    var str = $(':not(select[value=""])', form).serialize();

No erros, and the result is the entire form. The not() method gives the same.

What is possibly wrong?

EDIT: The question which was listed as possible duplicate to mine asks about possible implemntations for exlcuding empty fields frm serialization, while mine states that not() Selector doesn't work, asks why and for different to above mentioned solution.

Upvotes: 1

Views: 131

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115222

Which filters element with element attribute value which equals to an empty string and not the element with current value as an empty string.


For filtering element with current value as empty string use filter() method.

var str = $(':input', form).filter(function(){
   return this.value.trim() != '';
}).serialize();


UPDATE : If you just want to avoid empty select tags then do it like.

var str = $(':input', form).filter(function(){
   return !($(this).is('select') && this.value.trim() == '');
}).serialize();

Upvotes: 3

Related Questions