Jquery find input containing specific text

Asked

Viewed 230 times

1

Which selector jquery that I use to search for all inputs that contain specific text, for example I have several checkbox with different texts, ai type CA and it searches all checkbox with text that contains CA, after that I will display only those and hide the others.

2 answers

2


Ideally, your checkboxes have a value equal to the text you want to search for.

So you can use the jQuery queries

$("input[type='checkbox'][value*='Teste']")

For example

Look at the operator *= of the query, this is the same as "contains" or it looks for any part of your string. If you wanted it to be exactly the same text, use =.

Example in findler: https://jsfiddle.net/aLusvy63/

Edited

In case of using the data-text would be

$("input[type='checkbox'][data-text*='texto']")

More information about jQuery selectors on https://api.jquery.com/category/selectors/

  • cool, instead of putting text equal to id, can I use a date attribute ? example data-text="CA" ? if yes, how would it look?

  • @Henriquedpereira see my reply issue.

  • very good, that’s what I wanted, vlw.

0

You can do it this way:

$(":checkbox").filter(function() {
  return this.value == '5';
}).prop("checked","true");

or

$("input[type=checkbox][value=5]").prop("checked",true);​

DEMO: http://jsfiddle.net/59hHK/30/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.