JavaScript Multiple filterable select menus in JQuery mobile filterable multiple select with checkboxes

I am using JQuery Mobile version 1.4.2 and at some point in one of my templates I would like to use irs filterable select menu. However there is one problem:

That particular element has a specific id attached to it id="filter-menu" which means I can only use it once per template (for instance only for the list of apples).

Therefore I am asking: How may I use it more than one times in the same webpage?

Thanks in advance.

Answer:1

You can use any id you want as long as this id connects specific select and filter input.

Example: http://jsfiddle.net/8e5q9/

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
        <link rel="stylesheet" target='_blank' href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css" /> 
        <!--<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>-->
        <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>    
    </head>
    <body>     
        <div data-role="page" id="index" data-theme="a" >
            <div data-role="header">
                <h3>
                    First Page
                </h3>
                <a target='_blank' href="#second" class="ui-btn-right">Next</a>
            </div>

            <div data-role="content">
                <form>
                    <input type="text" data-type="search" id="filterable-input1"/>
                </form>
                <form data-role="controlgroup" data-filter="true" data-input="#filterable-input1">
                    <label for="pizza">
                        Pizza
                        <input type="checkbox" id="pizza"/>
                    </label>
                    <label for="goulash">
                        Goulash
                        <input type="checkbox" id="goulash"/>
                    </label>
                    <label for="falafel">
                        Falafel
                        <input type="checkbox" id="falafel"/>
                    </label>
                    <label for="spring-rolls">
                        Spring Rolls
                        <input type="checkbox" id="spring-rolls"/>
                    </label>
                </form>
                <form>
                    <input type="text" data-type="search" id="filterable-input2"/>
                </form>
                <form data-role="controlgroup" data-filter="true" data-input="#filterable-input2">
                    <label for="pizza">
                        Pizza
                        <input type="checkbox" id="pizza"/>
                    </label>
                    <label for="goulash">
                        Goulash
                        <input type="checkbox" id="goulash"/>
                    </label>
                    <label for="falafel">
                        Falafel
                        <input type="checkbox" id="falafel"/>
                    </label>
                    <label for="spring-rolls">
                        Spring Rolls
                        <input type="checkbox" id="spring-rolls"/>
                    </label>
                </form>    
            </div>

            <div data-role="footer" data-position="fixed">

            </div>
        </div> 
        <div data-role="page" id="second" data-theme="a" >
            <div data-role="header">
                <h3>
                    Second Page
                </h3>
                <a target='_blank' href="#index" class="ui-btn-left">Back</a>
            </div>

            <div data-role="content">

            </div>

            <div data-role="footer" data-position="fixed">

            </div>
        </div>  
    </body>
</html>    

Notice how in one case I'm using #filterable-input1 and in other case #filterable-input2.

Update

Again this was a peace of cake, next time try it yourself, I am not trying to make fun of you, this is how you'll learn to relay on yourself.

Working example: http://jsfiddle.net/Gajotres/zCq98/

HTML:

<form>
    <select id="filter-menu" data-native-menu="false">
        <option value="SFO">San Francisco1</option>
        <option value="LAX">Los Angeles1</option>
        <option value="YVR">Vancouver1</option>
        <option value="YYZ">Toronto1</option>
    </select>
</form>
<form>
    <select id="filter-menu2" data-native-menu="false">
        <option value="SFO">San Francisco2</option>
        <option value="LAX">Los Angeles2</option>
        <option value="YVR">Vancouver2</option>
        <option value="YYZ">Toronto2</option>
    </select>
</form>

JavaScript:

    $.mobile.document
    // "filter-menu-menu" is the ID generated for the listview when it is created
    // by the custom selectmenu plugin. Upon creation of the listview widget we
    // want to prepend an input field to the list to be used for a filter.
    .on( "listviewcreate", "#filter-menu-menu, #filter-menu2-menu", function( e ) {
        var input,
            listbox = $( "#filter-menu-listbox" ),
            form = listbox.jqmData( "filter-form" ),
            listview = $( e.target );
        // We store the generated form in a variable attached to the popup so we
        // avoid creating a second form/input field when the listview is
        // destroyed/rebuilt during a refresh.
        if ( !form ) {
            input = $( "<input data-type='search'></input>" );
            form = $( "<form></form>" ).append( input );
            input.textinput();
            $( "#filter-menu-listbox" )
            .prepend( form )
            .jqmData( "filter-form", form );
        }
        // Instantiate a filterable widget on the newly created listview and
        // indicate that the generated input is to be used for the filtering.
        listview.filterable({ input: input });
    })
    // The custom select list may show up as either a popup or a dialog,
    // depending how much vertical room there is on the screen. If it shows up
    // as a dialog, then the form containing the filter input field must be
    // transferred to the dialog so that the user can continue to use it for
    // filtering list items.
    //
    // After the dialog is closed, the form containing the filter input is
    // transferred back into the popup.
    .on( "pagebeforeshow pagehide", "#filter-menu-dialog", function( e ) {
        var form = $( "#filter-menu-listbox" ).jqmData( "filter-form" ),
            placeInDialog = ( e.type === "pagebeforeshow" ),
            destination = placeInDialog ? $( e.target ).find( ".ui-content" ) : $( "#filter-menu-listbox" );
        form
        .find( "input" )
        // Turn off the "inset" option when the filter input is inside a dialog
        // and turn it back on when it is placed back inside the popup, because
        // it looks better that way.
        .textinput( "option", "inset", !placeInDialog )
        .end()
        .prependTo( destination );
    });

How to:

It is not that hard. I have only changed this line:

.on( "listviewcreate", "#filter-menu-menu, #filter-menu2-menu", function( e ) {

and reason for this change were select box names: #filter-menu and #filter-menu2

#filter-menu  --> #filter-menu-menu
#filter-menu2 --> #filter-menu2-menu
Answer:2

I'm a bit of a beginner to JavaScript and I've been trying to figure this out for at least two hours. If someone could explain to me why this is happening, it'll be great! function slowDouble(x, ...

I'm a bit of a beginner to JavaScript and I've been trying to figure this out for at least two hours. If someone could explain to me why this is happening, it'll be great! function slowDouble(x, ...

  1. javascript settimeout loop variable

I have the following code: //Marks all users which are reading the book with the bookId var markAsReading = function (bookId,cb) { User.find({}, function (err,users) { if(err) ...

I have the following code: //Marks all users which are reading the book with the bookId var markAsReading = function (bookId,cb) { User.find({}, function (err,users) { if(err) ...

  1. nodejs loop wait
  2. nodejs loop wait for callback
  3. nodejs while loop wait
  4. node js for loop wait for promise

In my web app, running in an android WebView, whenever I click on something, or navigate somewhere, that container div is highlighted blue. Sometimes only for a moment, but sometimes until you click ...

In my web app, running in an android WebView, whenever I click on something, or navigate somewhere, that container div is highlighted blue. Sometimes only for a moment, but sometimes until you click ...

  1. android highlight webview
  2. android webview highlight text
  3. android webview highlight search text
  4. android webview highlight selected text
  5. android webview highlight color

I'm just wondering whether it is at all possible to transfer a directory from a unix server to my local machine using the ssh2 module in node.js. I have connected to the remote host and can read the ...

I'm just wondering whether it is at all possible to transfer a directory from a unix server to my local machine using the ssh2 module in node.js. I have connected to the remote host and can read the ...

  1. transfer entire directory scp
  2. transfer whole directory
  3. ftp transfer entire directory
  4. ftp command line transfer entire directory
  5. ftp command to transfer entire directory