Is there a built-in function/selector in jQuery to filter the inputs by value?
I can create a custom selector:
$.extend($.expr[":"], {
val: function(elem, index, match) {
return $(elem).val() == match[3];
}
});
and use it like this: $("input:val('Some text')")
.
I was just wondering if something like this already exists or maybe there is a better way.
Definition and Usage The :input selector selects form elements. This selector also works with the button element.
jQuery | filter() with ExamplesThe filter() method is used to filter out all the elements that do not match the selected criteria and those matches will be returned. Parameters: criteria : It specifies a selector expression, a jQuery object or one or more elements to be returned from a group of selected elements.
The jQuery filter() method can take the selector or a function as its argument to filters the set of matched elements based on a specific criteria.
jQuery not() Method Elements that do not match the criteria are returned from the selection, and those that match will be removed. This method is often used to remove one or more elements from a group of selected elements. Tip: The not() method is the opposite of the filter() method.
It doesn't look like there's a built-in solution. But for a one-off selector, .filter()
is an option:
$('input').filter(function() { return $(this).val() === 'Some text'; });
You can do this:
$('input[value=someval]')
you can also chain it
$('input[type=radio][value=someval]')
look here:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With