Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting select slider change event in jQuery Mobile

What is a reliable way to detect change of a select slider in jQuery Mobile? I try to bind a handler to change event of the select control itself, but it fires on initial page display, and then fires multiple times on clicks, and sometimes even on hover (in desktop browser).

The minimal working example of this behaviour is posted here: http://jsfiddle.net/NPC42/mTjtt/3/

This is probably caused by jQuery Mobile adding more elements to style the select as the flip-toggle, but I can't find the recommended way to do it.

Any help is much appreciated.

like image 824
NPC Avatar asked Aug 02 '11 10:08

NPC


3 Answers

May not be the slickest solution but it works http://jsfiddle.net/mTjtt/4/

like image 197
Calum Avatar answered Oct 20 '22 19:10

Calum


Live Example:

  • http://jsfiddle.net/KCQ4Z/14/
  • http://jsfiddle.net/phillpafford/KCQ4Z/70/ (using stopPropagation() )

JS:

$('#my-slider').change(function(event) {
    event.stopPropagation();
    var myswitch = $(this);
    var show     = myswitch[0].selectedIndex == 1 ? true:false;

    if(show) {            
        $('#show-me').fadeIn('slow');
        $('#first-me').fadeOut();
    } else {            
        $('#first-me').fadeIn('slow');
        $('#show-me').fadeOut();
    }
});

HTML:

<div data-role="page" id="home" class="type-home"> 
    <div data-role="content"> 
        <div class="content-primary"> 
            <p>The flip toggle switch is displayed like this:</p> 
            <div data-role="fieldcontain"> 
                <label for="slider">Flip switch:</label> 
                <select name="slider" id="my-slider" data-role="slider"> 
                    <option value="off">Off</option> 
                    <option value="on">On</option> 
                </select> 
            </div> 
            <div id="first-me">
                <p>

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
                </p>  
            </div>
            <div id="show-me" class="hidden">
                <p>
                    Bacon ipsum dolor sit amet bresaola velit laboris bacon eiusmod. Id ex short ribs, dolor dolore rump pork belly beef ad ullamco salami labore aute ut. Jowl et in do, fatback jerky salami reprehenderit irure laboris pork loin commodo qui eu. Chuck tri-tip cupidatat, turkey sunt in anim jerky pork belly exercitation bacon. Eu corned beef qui adipisicing, ground round veniam turkey chicken incididunt deserunt. Proident t-bone chuck, non excepteur biltong elit in anim minim swine short loin magna do. Sint enim nisi, minim nulla tongue ut incididunt ground round.
                </p>  
            </div>
        </div>
    </div>
</div>

UPDATE:

I have raised an issue/bug with jQM here:

  • https://github.com/jquery/jquery-mobile/issues/2188
like image 43
Phill Pafford Avatar answered Oct 20 '22 20:10

Phill Pafford


Use this code,

$( ".mySliders" ).slider({
    create: function (event, ui) {
        $(this).bind('change', function () {
            ...
            ...
        });
    }
});

!Do not put type="range" to your input tags , put type="text" instead.

Since you are calling slider function manually.

like image 1
nskeskin Avatar answered Oct 20 '22 21:10

nskeskin