I have a number of textfields and I need to select the values of those textfields whose values are not equal to their title
attribute.
Problem: With my attempt, the jQuery code below simply selects the value of the first matched textfield. How do I get all matched textfields' values?
jQuery Code
var inputs = $(".input").filter(function() {
return $(this).val() != $(this).attr('title');
}).val();
console.log(inputs);
I have a number of textfields and I need to select the values of those textfields whose values are not equal to their title
attribute.
Problem: With my attempt, the jQuery code below simply selects the value of the first matched textfield. How do I get all matched textfields' values?
jQuery Code
var inputs = $(".input").filter(function() {
return $(this).val() != $(this).attr('title');
}).val();
console.log(inputs);
Share
Improve this question
asked Jan 31, 2012 at 5:52
NyxynyxNyxynyx
63.7k163 gold badges506 silver badges856 bronze badges
3 Answers
Reset to default 6Here is simpler solution:
var input = [];
jQuery('input[type=text]').each(function(){
if(jQuery(this).val() != jQuery(this).attr('title') ) {
input.push(jQuery(this).val());
}
});
The .val()
method only returns the value of the first element in the selected list of elements. Try turning it into an array instead:
var inputs = $(".input").filter(function() {
return $(this).val() != $(this).attr('title');
}).map(function(){
return $(this).val();
}).get();
console.log(inputs);
Here's one way to do it:
var inputs = [];
$(".input").filter(function() {
return $(this).val() != $(this).attr('title');
}).each(function() {
inputs.push($(this).val());
});
console.log(inputs);
Example: http://jsfiddle/grc4/cCRfE/