Using jQuery filter () to get .val () match

I have several text fields, and I need to select the values ​​of those text fields whose values ​​are not equal to their title attribute.

Problem: With my attempt, the jQuery code below simply selects the value of the first matching text field. How to get values ​​of matching text fields?

JQuery code

 var inputs = $(".input").filter(function() { return $(this).val() != $(this).attr('title'); }).val(); console.log(inputs); 
+4
source share
3 answers

Here is a simpler solution:

 var input = []; jQuery('input[type=text]').each(function(){ if(jQuery(this).val() != jQuery(this).attr('title') ) { input.push(jQuery(this).val()); } }); 
+5
source

The .val() method returns only the value of the first element in the selected list of elements. Try turning it into an array:

 var inputs = $(".input").filter(function() { return $(this).val() != $(this).attr('title'); }).map(function(){ return $(this).val(); }).get(); console.log(inputs); 
+3
source

Here is one way to do this:

 var inputs = []; $(".input").filter(function() { return $(this).val() != $(this).attr('title'); }).each(function() { inputs.push($(this).val()); }); console.log(inputs); 

Example: http://jsfiddle.net/grc4/cCRfE/

+3
source

Source: https://habr.com/ru/post/1393906/


All Articles