Jquery Select all items with a common data attribute name

I am trying to use jQuery to select all the inputs in my form that have a data attribute with a similar name.

This is my form:

<form id = "myForm"> <input name="name" data-valid-presence=true > <input name="age" data-valid-biggerThan="18" > <input name="email[0]" data-valid-email=true > <input name="email[1]" data-valid-email=true > </form> 

and my jQuery selector:

 var inputs = jQuery("#myForm").find("input[data-valid-email],[data-valid-length],[data-valid-presence], [data-valid-biggerThan]"); 

I am looking for a way to select all the inputs that have data-valid-* in them without finding them one by one like this.

Any ideas?

+4
source share
1 answer

You can use jQuery.filter:

 var inputs = $('form').find('input').filter(function() { var matched = false; $.each(this.attributes, function(attr) { if ( matched ) return; matched = /^data-valid/.test(this.name); }); return matched; }); 
+1
source

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


All Articles