I am using the following jQuery code to count the number of text fields that have a value that is the same as its title attribute.
$(".textfield").val( $(".textfield").attr('title') ).size();
Problem: How do I count the number of text fields that do not have a value equal to its title attribute?
First of all, the code you post is not doing what you say it does. This:
Will set the value of all elements with class “.textfield” to the title of the first “.textfield” element’s title attribute, and then return a count of all of them.
You need to compare the return of the
.val()method (with no parameter it returns the current value) with the return of the.attr('title')method. You can do this with.filter()and then check the.lengthof the resulting jQuery object:And then to get a count of those where the value is not equal just do the same thing except with
!=instead of==. So:(Note that
.lengthwill give you the same count as.size()but without the overhead of a function call.)