Using jQuery to filter on table cells ...

Posted on 2012-08-24
Last Modified: 2012-08-25
Hey all.

I have a table and a search field. I want to use jQuery to search the table cells and apply a class to the cell(s) that match the search term, case insensitive and using "matches".

This is what I've got.

$("table tbody tr").find('td:eq(2)').filter(function(e){
            var searchterm = $(this).text().toLowerCase();
            var searchtext = term.toLowerCase();

            $(this).text().toLowerCase() == term.toLowerCase();
            //return searchterm.match('/\b'+searchtext+'/gi');

This code works if I give complete string. Exampe: if cell contains "test one", I have to enter "test one". I want to just enter "test" or just "one" and still get a hit.

Any ideas? That regular expression I would expect to work but to return it is not working.
Question by:Webspeeder
    LVL 25

    Accepted Solution

          $("table tbody tr").find('td').filter(function(e){
                var searchterm = $(this).text().toLowerCase();
                var searchtext = term.toLowerCase();
                var patt=new RegExp(searchtext,'gi');
                return patt.test(searchterm)

    Author Comment

    I think that did the trick. Thank you very much.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now