Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

:contain / start by

Tags:

jquery

filter

Is there a "start by" filter in jQuery, something like :contain but with a string beginning condition ?

like image 926
bdo334 Avatar asked Jan 06 '10 10:01

bdo334


2 Answers

Not that I know of, but you can easily implement your own selector for jQuery:

$.extend($.expr[':'], {
    startsWith: function(elem,match) {  
        return (elem.textContent || elem.innerText || "").indexOf(match[3]) == 0;
    }  
});

Now you can use it like this:

$("p:startsWith(Hello)").css("color","red")
like image 145
duckyflip Avatar answered Sep 27 '22 19:09

duckyflip


No, but you can do it yourself using filter, as pulse suggested:

$('a').filter(function(){
    return $(this).text().indexOf('start') == 0 }
)

You may want to use a regular expression here, to ignore case, or for more advanced searches:

$('a').filter(function(){
    return $(this).text().match(/^start/i) }
)
like image 29
Kobi Avatar answered Sep 27 '22 18:09

Kobi