59

How can I check for empty values of (required) input fields within a section, and then add a class to them on an event, using jQuery? So far, I have tried:

jQuery("#sender_container input.required").val("").addClass("error");

But that seems to SET the value, rather than checking it. Any ideas?

0

7 Answers 7

117
jQuery("#sender_container input.required").filter(function() {
    return !this.value;
}).addClass("error");​

Why you have to use filter and not [value=""] you can see in this DEMO

The reason is: attribute selectors check the initial state of the element, not the current state. (note that you can change the "initial" state with the attr function, but it's bad practice, you should always use prop)

So if you change the input value, the current value won't effect the attribute selector. not wise... :)

Notes:

  • .val() returns the value of the form element, and breaks the jQuery chain, $('selector').val().addClass('foo') Error, the return value is a string\ number

  • .val(valueToSet) sets the value of the form element and doesn't break the jQuery chain.
    $('selector').val("some value").addClass('foo') - Valid, the returned value is a jQuery

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

12 Comments

Thanks but this only works if ALL the inputs are empty. But if I have 2 required fields, and one has a value and the other is empty, nothing happens.
@RichardHarris. It should work fine in that case as well. there is something else in your code. see this demo
@RichardHarris. Or this demo used the css of codeparadox
Hmm. Trying to figure out why it doesn't work for me. Btw, your demo code is invalid because your div container closes immediately <div id="foo" />. Thanks for the help so far! Will update here when I can get it to work for me. Cheers!
@personne3000, if it's not documented as officially supported, I would never use it as it looks a bit hacky. But if it does, that's a great way! Thank you for proving me wrong sir.
|
11
$('input:text[value=]','#sender_container').addClass('error');

DEMO

1 Comment

This will not work in jQuery 1.9+, see jquery.com/upgrade-guide/1.9/#attr-versus-prop-
3
$('#sender_container input.required[value=""]').addClass('error')

2 Comments

Actually it will not work at all, there is a wrong space between the input and the attribute selector... :) it will search for children of input with an empty value attribute, input can't have children...
Thanks, Parv. Your code should now work for any children of a containing element with id sender_container that is an input field using class required and whose value is empty, correct?
2
jQuery('#sender_container input.required[value=""]').addClass("error");

You can try this:

$('input:not([value!=""])').addClass('error');

DEMO

Note: This answer should not be used, and the only reason it wasn't deleted is so it can be learned from.

10 Comments

either the inner or outer quotes need to be single, or escape the inner quotes.
@gdoron: Never mind, I tried it, it didn't work... strange, must have been something else then. But it definitely was related to the selectors and input field values ;)
@gdoron: I think it's the != and that's probably what I was referring to earlier, but could not properly remember. I assume, since [attr!=value] is not a CSS3 selector (afaik), it's evaluated by Sizzle and it seems to take the actual value. @thecodeparadox: Only because it works, does not mean it should be used. Double negations are difficult to understand as well.
@gdoron yup, will never use it. should I remove this post or keep here to make another aware
@thecodeparadox Leave the code, it's a good learning tool, for future reference.
|
0
$field = $("#sender_container input.required");
if( ! $field.val())
{
    $field.addClass("error");
}

this simple way may work.

1 Comment

$("#sender_container input.required").each(function( if( ! $(this).val()) { $(this).addClass("error"); })) try this.
0

If you only need to select based on the initial attribute value of the input then the following will do:

var elements = $('#sender_container input.required[value=""]')

But be aware that this won't work if the value attribute isn't present. It also won't work for the current input value if it has been changed by user or script.

If you'd like to get the current input value you can use jquery's filter function:

var elements = $('#sender_container input.required').filter(function() {
  return this.value === '';

  // alternatively for "no value":
  // return !this.value;
})

After you've selected the jquery elements you can add your class:

elements.addClass('error');

Comments

0

to get all fields inspected this might help.

$('#sender_container [required]').each(function(index)
{
       if (!($(this).val())) $(this).addClass('error');
}

});

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.