When you use val , you change the value property. When you use attr , you change the attribute.
When you use an attribute selector to select an element, it will only work if the attribute is present.
If you need to select elements based on the value of their value property, you can use filter :
$("input").filter(function() { return this.value === "whatever";
source share