Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$("input:not(:empty)") is not working

Tags:

jquery

<html>
<head>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    alert('hi'+$("input:text").val());
    $("input:not(:empty)").val("sdfdf");
  });
});
</script>
</head>
<body>
<input type="text" value="aa" />
<input type="text" value="" />
<input type="text" value="aa" />
<button>Click me</button>
</body>
</html>

i am trying to access empty textboxex using jquery and assigning a value hello to it.. but it's not working .

thanks in advance

like image 909
user1194147 Avatar asked Feb 07 '12 09:02

user1194147


People also ask

How to check if all inputs are filled jQuery?

Just use: $("input:empty"). length == 0; If it's zero, none are empty.

Is Empty is a button state in CSS?

The :empty CSS pseudo-class represents any element that has no children. Children can be either element nodes or text (including whitespace). Comments, processing instructions, and CSS content do not affect whether an element is considered empty.


1 Answers

:empty checks for whether an element has child elements. input elements cannot have child elements.

If you want to test that the input element's value is blank:

$(document).ready(function(){
  $("button").click(function(){
    $("input").filter(function() {
        return this.value.length !== 0;
    }).val("sdfdf");
  });
});

There we get all of the input elements, and then filter it so only the ones whose value property isn't "" are included.

like image 77
T.J. Crowder Avatar answered Sep 28 '22 11:09

T.J. Crowder