Robert
Robert

Reputation: 10390

jquery filtering selections with the filter function

So according to this jQuery documentation "In order to get the best performance using :password, first select elements with a standard jQuery selector, then use .filter( ":password" ), or precede the pseudo-selector with a tag name or some other selector."

So I comply and filter out the selection using the filter function but it returns 0; no elements were selected. Why is this?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>jQuery</title>
<script src="jquery-1.9.1.js"></script>
<script>
$(document).ready(function() {

    var t = $("form").filter(":password");

    $("body").append(t.length);


});
</script>
</head>
<body>
<form action="" method="post">
<input type="password" name="file" />
</form>
</body>
</html>

Upvotes: 0

Views: 103

Answers (2)

Ken Herbert
Ken Herbert

Reputation: 5236

Because you don't have any forms of type password.

You could use one of the following: $('form input').filter(":password") or $("form").find("input:password")

Upvotes: 0

Rob Hruska
Rob Hruska

Reputation: 120286

You'll want to query <input> elements and filter on those:

$('form input').filter(':password');

Right now you're selecting all <form> elements and filtering them for :password types (and there won't be any).

Upvotes: 2

Related Questions