0

I'm writing a method that generate the string selector to use to bind an input event on an element.

I'm facing to a little problem, for now my code works but not perfectly. If not_validated = true then it doesn't matter the value of valid/invalid because my select will select them anyway. I'm looking for a way to exclude the elements that have some classes. See the TODO in the source code. Probably something with ^ or ! but I only know the ways that use methods such as $.hasClass and so on.

FormValidator.prototype.defaultOptions = {
  // Performs live validation, on value change.
  live: {
    not_validated: true,
    valid: true,
    invalid: true
  }
};

// Stuff ...

FormValidator.prototype._generateSelectorOnInput = function() {
  var selector = '';

  if(this.options.live && this.options.live.not_validated){
    selector += '[data-validate]'// TODO: Need to exclude the selector that have INPUT_SUCCESS_CLASS or INPUT_ERROR_CLASS !
  }

  if(this.options.live && this.options.live.valid){
    selector += (selector.length ? ', ' : '') + '[data-validate].' + INPUT_SUCCESS_CLASS
  }

  if(this.options.live && this.options.live.invalid){
    selector += (selector.length ? ', ' : '') + '[data-validate].' + INPUT_ERROR_CLASS
  }

  return selector;
};
2
  • 2
    The ':not()' selector or .not() method may be of interest to you. Commented Apr 4, 2014 at 13:54
  • 1
    See .not(). Usage in your case will be $('[data-validate]').not('.INPUT_SUCCESS_CLASS, .INPUT_ERROR_CLASS') api.jquery.com/not Commented Apr 4, 2014 at 13:56

1 Answer 1

1

Simply use :not()

selector += '[data-validate]:not(.' + INPUT_SUCCESS_CLASS + ', .' + INPUT_ERROR_CLASS + ')';
Sign up to request clarification or add additional context in comments.

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.