Johannes Scherg
Johannes Scherg

Reputation: 129

Speeding up this jQuery-code with selectors

i wrote a filter for a list of many objects. Users can choose their options in via radio-buttons.

Within my filter functionality i select the checked radio buttons of both radio groups/sets:

if ( $('#filter input[name="interval"]:checked').val() == 'all' ) {
    if( $('#filter input[name="day"]:checked').val() == 'all' ) {
        var searchSelector = "li";
    } else {
        var searchSelector = "li[data-day=" + $('#filter input[name="day"]:checked').val() + "]";
    }
} else {
    if( $('#filter input[name="day"]:checked').val() == 'all' ) {
        var searchSelector = "li[data-interval=" + $('#filter input[name="interval"]:checked').val() + "]";
    } else {
        var searchSelector = "li[data-interval=" + $('#filter input[name="interval"]:checked').val() + "]" + "li[data-day=" + $('#filter input[name="day"]:checked').val() + "]";
    }
}

As it's said in the jQuery Performance Rules i should cache jQuery objects. Usually this practice always works — but in this case the values of my second radio buttons always outputs the value of the first checked value.

Any ideas, how i can simplify this jQuery code?

THANKS

Here is the HTML for the buttons:

<form id="filter">
    <fieldset>
        <label>
            <input type="radio" name="interval" value="all">
            all</label>
        <label>
        <label>
            <input type="radio" name="interval" value="hourly">
            hourly</label>
        <label>
            <input type="radio" name="interval" value="daily">
            daily</label>
        <label>
            <input type="radio" name="interval" value="weekly">
            weekly</label>
        <label>
            <input type="radio" name="interval" value="monthly">
            monthly</label>
        <label>
            <input type="radio" name="interval" value="yearly">
            yearly</label>
    </fieldset>
    <fieldset>
        <label>
            <input type="radio" name="day" value="all">
            all</label>
        <label>
            <input type="radio" name="day" value="monday">
            monday</label>
        <label>
            <input type="radio" name="day" value="tuesday">
            tuesday</label>
        <label>
            <input type="radio" name="day" value="wednesday">
            wednesday</label>
        <label>
            <input type="radio" name="day" value="thursday">
            thursday</label>
        <label>
            <input type="radio" name="day" value="friday">
            friday</label>
    </fieldset>
</form>

And this is what my list looks like:

<li data-day="monday" data-interval="weekly">foo</li>
<li data-day="friday" data-interval="yearly">foo</li>

Upvotes: 2

Views: 167

Answers (2)

gnarf
gnarf

Reputation: 106332

Why not do something like this -- I am assuming that the searchSelector is being applied to a .find() later in the code as there is no ul defined to look within...

// This may also be $("li") -- I was assuming you were using it for a .find()
var $result = $something.find("li");
// This finds "all" the possible results - now to filter we need the values:
// Use || 'all' to default to "all" if there is no "checked" filter
var interval = $('#filter input[name="interval"]:checked').val() || 'all';
var day = $('#filter input[name="day"]:checked').val() || 'all';

// if we need to filter this list at all:
if (day != 'all' || interval != 'all') {
  // store the new result of the filter
  $result = $result.filter(function() {
    // check the .data-day .data-interval attributes on this directly:
    if (day != 'all' && this.data-day != day) return false;
    if (interval != 'all' && this.data-interval != interval) return false;
    return true;
  });
}

I am also assuming you are using this to toggle the visibility of these <li> somehow... In the first line you could attach .hide() to the chain and call $results.show() at the end of this block of code

Upvotes: 1

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195982

The following should do it

var $interval = $('#filter input[name="interval"]'),
    $day = $('#filter input[name="day"]');

    $('#filter input:radio').change(function(){
        var interval = $interval.filter(':checked').val() || 'all',
            day = $day.filter(':checked').val()|| 'all',
            searchSelector = 'li';

            searchSelector += (day != 'all') ? '[data-day=' + day  + ']' : '';
            searchSelector += (interval != 'all') ? '[data-interval=' + interval  + ']' : '';
          }
        /*do what you want with the selector here*/
    });

example: http://www.jsfiddle.net/gaby/xPMn5/

Upvotes: 2

Related Questions