1

I am learning jQuery, while reading book, there is a code, where you are making custom selector. Here is the code

(function($) {
    $.extend($.expr[':'], {
        group: function(element, index, matches, set) {
            var num = parseInt(matches[3], 10);
            if (isNaN(num)) {
                return false;
            }
            return index % (num * 2) < num;
        }
    });
})(jQuery);

and here it is calling

$(document).ready(function() {
    function stripe() {
        $('#news').find('tr.alt').removeClass('alt');
        $('#news tbody').each(function() {
            $(this).children(':visible').has('td')
                .filter(':group(3)').addClass('alt');
        });
    }

    stripe();

});

I know that .filter() runs for each sub element. suppose that if my tbody element has 4 tr(rows), then .filter runs for each tr(implicit iteration). Now when call .filter(':group(3)'), then i notice that the parameters that passes to :group function are.

First time it become

group: function(element, index, matches, set)

element is tr , index is 0, mathces become [":group(3)", "group", "", "3"] and set become [tr, tr, tr, tr, tr]

Now i undrstand set, that each tbody tag has number of tr, that become set array. But please also tell me that how parseInt(matches[3], 10) is working?

I want to ask how jQuery made matches array. I just write :group(3). How matches value is setting to these values in the parameter list?

Here is the snippet of html

<table id="news">
    <thead>
        <tr>
            <th>Date</th>
            <th>Headline</th>
            <th>Author</th>
            <th>Topic</th>
        </tr>
    </thead>

    <tbody>
        <tr>
            <th colspan="4">2011</th>
        </tr>
        <tr>
            <td>Apr 15</td>
            <td>jQuery 1.6 Beta 1 Released</td>
            <td>John Resig</td>
            <td>Releases</td>
        </tr>
        <tr>
            <td>Feb 24</td>
            <td>jQuery Conference 2011: San Francisco Bay Area</td>
            <td>Ralph Whitbeck</td>
            <td>Conferences</td>
        </tr>
        <tr>
            <td>Feb 7</td>
            <td>New Releases, Videos &amp; a Sneak Peek at the jQuery UI Grid</td>
            <td>Addy Osmani</td>
            <td>Plugins</td>
        </tr>
        <tr>
            <td>Jan 31</td>
            <td id="release">jQuery 1.5 Released</td>
            <td>John Resig</td>
            <td>Releases</td>
        </tr>
        <tr>
            <td>Jan 30</td>
            <td>API Documentation Changes</td>
            <td>Karl Swedberg</td>
            <td>Documentation</td>
        </tr>
    </tbody>

    <tbody>
        <tr>
            <th colspan="4">2010</th>
        </tr>
        <tr>
            <td>Nov 23</td>
            <td>Team Spotlight: The jQuery Bug Triage Team</td>
            <td>Paul Irish</td>
            <td>Community</td>
        </tr>
        <tr>
            <td>Oct 4</td>
            <td>New Official jQuery Plugins Provide Templating, Data Linking and Globalization</td>
            <td>John Resig</td>
            <td>Plugins</td>
         </tr>
         <tr>
             <td>Sep 4</td>
             <td>The Official jQuery Podcast Has a New Home</td>
             <td>Ralph Whitbeck</td>
             <td>Documentation</td>
         </tr>
         <tr>
             <td>Aug 24</td>
             <td>jQuery Conference 2010: Boston</td>
             <td>Ralph Whitbeck</td>
             <td>Conferences</td>
         </tr>
         <tr>
             <td>Aug 13</td>
             <td>The jQuery Project is Proud to Announce the jQuery Mobile Project</td>
             <td>Ralph Whitbeck</td>
             <td>Plugins</td>
         </tr>
         <tr>
             <td>Jun 14</td>
             <td>Seattle jQuery Open Space and Hack Attack with John Resig</td>
             <td>Rey Bango</td>
             <td>Conferences</td>
         </tr>
         <tr>
             <td>Mar 16</td>
             <td>Microsoft to Expand its Collaboration with the jQuery Community</td>
             <td>John Resig</td>
             <td>Miscellaneous</td>
         </tr>
         <tr>
             <td>Mar 15</td>
             <td>jQuery Conference 2010: San Francisco Bay Area</td>
             <td>Mike Hostetler</td>
             <td>Conferences</td>
         </tr>
         <tr>
             <td>Jan 14</td>
             <td>jQuery 1.4 Released</td>
             <td>John Resig</td>
             <td>Releases</td>
         </tr>
         <tr>
             <td>Jan 8</td>
             <td>14 Days of jQuery and the New API Browser</td>
             <td>John Resig</td>
             <td>Documentation</td>
         </tr>
     </tbody>

     <tbody>
         <tr>
             <th colspan="4">2005</th>
         </tr>
         <tr>
             <td>Dec 17</td>
             <td>JSON and RSS</td>
             <td>John Resig</td>
             <td>Documentation</td>
         </tr>
         <tr>
             <td>Dec 14</td>
             <td>Google Homepage API</td>
             <td>John Resig</td>
             <td>Miscellaneous</td>
         </tr>
         <tr>
             <td>Dec 13</td>
             <td>XPath and CSS Selectors</td>
             <td>John Resig</td>
             <td>Miscellaneous</td>
         </tr>
         <tr>
             <td>Dec 12</td>
             <td>Sparklines with JavaScript and Canvas</td>
             <td>John Resig</td>
             <td>Miscellaneous</td>
         </tr>
     </tbody>

 </table>

Thanks

1 Answer 1

1

In your example, matches[3] == "3". So parseInt(matches[3], 10); will convert "3" to an integer 3 (base 10 = decimal).

Sign up to request clarification or add additional context in comments.

5 Comments

hhmm ok thanks :) and what about the array creation [":group(3)", "group", "", "3"] ?
that depends on how it is implemented. I think it will be some kind of regular expression with () grouping parentheses. Every group of the regex will be one entry of the array. Maybe someone else can shed some light on that.
I found this explanation but i didn't understand what it is saying matches: An array containing the result of the regular expression that was used to parse this selector. Typically, matches[3] is the only relevant item in the array; in a selector of the form :a(b), the matches[3] item contains b, the text within the parentheses. can you explain it to me ? Thanks
(Very) short example. Let's say you have the regex /(\d+)[a-z]/. It will match strings that have a number of digits followed by a single lower case character. If you call var myMatch = "123d".match(/(\d+)[a-z]/) it will give you an array containing 2 elements. The first element indexed [0] will be the full matched string, the second element indexed [1] will be the match for the first group (\d+). So the array will be ["123d", "123"]
hhmmm it means internally jQuery perform some rejex test, and on the basis of that test it generates this array. Is it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.