3

Hi is there anyway to search text in dom, as we do for SQL query LIKE?

i mean, i have.

<ul>
<li>ab</li>
<li>abrecot</li>
<li>abus</li>
<li>aby</li>
<li>abrea</li>
</ul>

i would like to search for "abre" and so return in text ASC order:

<li>abrea</li>
<li>abrecot</li>

is this possible?

definitely the query would looks like doing:

SELECT <li> FROM <ul> WHERE text LIKE 'abre%' ORDER BY text ASC; :))
itsme
  • 48,972
  • 96
  • 224
  • 345
  • 1
    It's not a duplicate, but this may give you some ideas: http://stackoverflow.com/questions/5324798/how-to-search-an-array-in-jquery-like-sql-like-value-statement – Kiley Naro Sep 27 '11 at 13:19
  • uhm it search in array wich will be an array of elements, but he search for %text% i would like to search for text% :P – itsme Sep 27 '11 at 13:21

3 Answers3

6

As you are looking for elements whose text starts with a specified string, you can use the filter method:

var x = $("ul li").filter(function() {
    return $(this).text().substr(0, 4) === "abre";
});

This will only return elements which have the string "abre" at the start. The other answers using the contains method will return elements with the string found anywhere within them, which does not match your pseudo-SQL query.

Here's a working example of the above.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
5

I think you would want the jQuery 'contains' function, have a look at the docs here:

http://api.jquery.com/contains-selector/

Your example would probably look like this:

$("li:contains('abre')")

EDIT to include the comments here, if you are looking for "starts with", you can do this on an element using the following syntax:

$('li[anAttribute^="abre"]')

But this assumes you have an attribute to query which i don't think you do, in which case the filters answer will likely suit your needs best.

dougajmcdonald
  • 19,231
  • 12
  • 56
  • 89
  • wonderfull, i was also reading here http://stackoverflow.com/questions/2220851/element-or-class-like-selector-for-jquery – itsme Sep 27 '11 at 13:24
  • 1
    That will return elements containing the string anywhere, but the OP wants only elements starting with the string. – James Allardice Sep 27 '11 at 13:25
  • but what i'm afraid to, is that this searches %text% while i would like to search text% :P – itsme Sep 27 '11 at 13:25
  • I take your point and his psuedo query would indicate this, but when I read his text it said "search for "abre"" which i assumed to be a check for the existence of a string inside an element, rather than specifically starts with. – dougajmcdonald Sep 27 '11 at 13:27
4

The :contains() selector is probably what you're looking for:

http://api.jquery.com/contains-selector/

QUOTE:

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<div>John Resig</div>

<div>George Martin</div>
<div>Malcom John Sinclair</div>
<div>J. Ohn</div>


<script>
$("div:contains('John')").css("text-decoration", "underline");
</script>

</body>
</html>
Kiley Naro
  • 1,749
  • 1
  • 14
  • 20