Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function/selector to filter inputs by value

Tags:

jquery

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.

like image 303
Corneliu Avatar asked Jul 06 '11 14:07

Corneliu


People also ask

What's an input selector?

Definition and Usage The :input selector selects form elements. This selector also works with the button element.

How to filter value in jQuery?

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.

Which type of parameter can jQuery filter function take?

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.

What is the following syntax is used to apply not equal filter on HTML elements using jQuery?

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.


2 Answers

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'; });
like image 171
Karl Horky Avatar answered Sep 27 '22 18:09

Karl Horky


You can do this:

    $('input[value=someval]')

you can also chain it

    $('input[type=radio][value=someval]')

look here:

like image 29
Nicola Peluchetti Avatar answered Sep 27 '22 17:09

Nicola Peluchetti