Jquery refer to a dom input with null value

Im trying to refer to all inputs with nill value. Tried with below code and failed. I was trying to change its css thats wy used .css

$("('input').val()=''").css('border','1px solid red');

Answers 1

  • It's not valid to put JS function calls inside a string for use as a selector.

    What you can do is select all inputs and then use the .filter() method to reduce the set to only those with a blank value:

    $('input').filter(function() {
      return this.value === '';
    }).css('border','1px solid red');
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <input value="Pineapple"> <input value=""> <input value="Orange">
    <input value=""> <input value="Mango"> <input value="Lime">

    You may want to use return $.trim(this.value) === '' if fields with space characters in them count as empty for your purposes.


Related Articles