0

I have a popup list in jquery mobile, with two items you can click to select. I can't get jquery mobile to "remember" -- via class='ui-btn-active' -- which listview item has been selected. You click on a list item, but the ui-btn-active class doesn't get attached to it.

This is the jquery I'm using (which doesn't work):

$('a.switchview').on('click', function () {
    $(this).addClass('ui-btn-active ui-state-persist');
});

And here's the HTML:

<div data-role='page' data-add-back-btn='true'>
    <a href='#popupSwitchView' class='ui-state-persist ui-btn ui-shadow ui-corner-all ui-icon-eye ui-btn-icon-notext ui-alt-icon  ui-nodisc-icon ui-btn-inline' data-transition='slideup' data-rel='popup' data-inline='true'>Switch view</a>

    <div data-history='false' data-role='popup' id='popupSwitchView'>
<a href='#' data-rel='back' data-role='button' data-icon='delete' data-iconpos='notext' class='ui-btn-right' data-theme='a'>Close</a>

        <ul data-role='listview' data-icon='delete'>
            <li data-role='list-divider' style='font-style: oblique;'>Select choice</li>
            <li><a href='javascript:;' data-role='button' data-transition='fade' data-mini='true' class=' switchview' id='choiceA' name='theView' value='choiceA'>Choice A</a>

            </li>
            <li><a href='javascript:;' data-role='button' data-transition='fade' data-mini='true' class='switchview' id='choiceB' name='theView' value='choiceB'>Choice B</a>

            </li>
        </ul>
    </div>
</div>

FIDDLE

Any ideas why this isn't working?

1 Answer 1

0

Return false from the click event and remove active class from previous selection:

$('a.switchview').on('click', function () {
    $('a.switchview').removeClass('ui-btn-active ui-state-persist');
    $(this).addClass('ui-btn-active ui-state-persist');
    return false;
});

Updated FIDDLE

1
  • ah, nice! thanks -- works perfectly in a desktop browser. However, in mobile (blackberry, iPhone 6) the state doesn't persist when the popup is closed then reopened
    – prokaryote
    Nov 30, 2015 at 18:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.