2

I have a list like this

<ul>

<li language="English"> Australia </li>
<li language="English"> America </li>
<li language="French"> France </li>
<li language="French"> Canada </li>
<li langauge="German"> Germany </li>

</ul>

I just want to filter and display the list having language as English. How can I achieve this using Jquery?

And what would be the strategy if the li has multiple attributes like <li language="English,French"> Canada </li>, I want to display Canada for both English as well as French language speaking country.

Appreciate if you post exact code as I already played with hide() and not() for a while now.

2
  • What? Do you want to hide the language="English" elements, or show them? If you want to show Canada as well, then add all the relevant attributes/attribute-values, and explain which should be shown and which hidden. Commented May 13, 2013 at 18:26
  • Refer this answer Commented May 13, 2013 at 18:45

6 Answers 6

4

Something like this:

<li language="English"> Australia </li>
<li language="English"> America </li>
<li language="French"> France </li>
<li language="French English"> Canada </li>
<li langauge="German"> Germany </li>

$('li').not("[language*='English']").hide();
Sign up to request clarification or add additional context in comments.

Comments

1

Should go like this, using the "contains" attribute selector http://api.jquery.com/category/selectors/attribute-selectors/ -

$('li').not('[language*="English"]').hide();

Here is a fiddle - http://jsfiddle.net/caySY/

4 Comments

This wont work as it hid all the li elements. Some li dont have the language attribute. I dont want to touch them and leave as it is.
No, it couldn't have hid them all - please see the fiddle.
Ok thanks it worked, so what if I have two lists like this on a page and I want the filtering on one list and not on other
You would specify which list. For instance you would use $('ul').eq(0) for the first one, $('ul').eq(1) for the second one. The index number relates to which list in the page you need to refer to, or you can given each list an id.
1

Just do this way

$("li:not([language*='English'])").hide();

Refer LIVE DEMO

HTML:

<ul>
    <li language="English"> Australia </li>
    <li language="English"> America </li>
    <li language="French"> France </li>
    <li language="French"> Canada </li>
    <li langauge="German"> Germany </li>
    <li language="English,French"> Canada1 </li>
</ul>

OUTPUT:

<li language="English"> Australia </li>
<li language="English"> America </li>
<li language="English,French"> Canada1 </li>

Comments

1

A relatively simple solution, using a select element to handle the language-choice:

$('#language').change(function(){
    // storing the language chosen in the select element
    var v = this.value;
    /* iterates over all li elements, and hides them,
       then filters the matched elements to see which elements
       contain the relevant language in the 'language' attribute: */
    $('ul li').hide().filter(function(i){
        return this.getAttribute('language').indexOf(v) !== -1;
    // shows the elements that contain the relevant language:
    }).show();
});

JS Fiddle demo.

Or a slightly different approach:

$('#language').change(function () {
    var v = this.value;
    $('ul li').each(function(){
        var l = this.getAttribute('language');
        return $(this).toggle(l.indexOf(v) !== -1);
    });
});

JS Fiddle demo.

Both the above work with the following HTML:

<label for="language">Show:</label>
<select name="language" id="language">
    <option>English</option>
    <option>French</option>
    <option>German</option>
</select>
<ul>
    <li language="English">Australia</li>
    <li language="English">America</li>
    <li language="French">France</li>
    <li language="English French">Canada</li>
    <li language="German">Germany</li>
</ul>

Incidentally please note the corrected spelling of 'language' in the final li element.

Further, it'd be better to correct your HTML to use a valid (under HTML5) data-* attribute, such as data-language (to use the obvious):

<ul>
    <li data-language="English">Australia</li>
    <li data-language="English">America</li>
    <li data-language="French">France</li>
    <li data-language="English French">Canada</li>
    <li data-language="German">Germany</li>
</ul>

And the above code amended, to use that modified HTML:

$('#language').change(function(){
    // storing the language chosen in the select element
    var v = this.value;
    /* iterates over all li elements, and hides them,
           then filters the matched elements to see which elements
           contain the relevant language in the 'language' attribute: */
        $('ul li').hide().filter(function(i){
            return $(this).data('language').indexOf(v) !== -1;
            // shows the elements that contain the relevant language:
        }).show();
    });

JS Fiddle demo.

$('#language').change(function () {
    var v = this.value;
    $('ul li').each(function(){
        var self = $(this),
            l = self.data('language');
        return self.toggle(l.indexOf(v) !== -1);
    });
});

JS Fiddle demo.

References:

Comments

0

You can use Attribute selector:-

$('li[language]').hide(); //hide all with attribute language
$('li[language=English]').show() //show the ones with English

1 Comment

Why take two lines when you can do it with one?
0

If you have multiple string in your language selector use the contains selector

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

$('li[language*="English"]').hide();

Comments

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.