Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you remove a button's active state with jQuery Mobile?

In my mobile app, using jQuery Mobile...

  • I would like to make a simple button execute a simple javascript function on click. No page transitions, nothing special like that.

  • I understood I can eliminate the page transitions by doing return false or preventDefault()

  • But the problem is the button sticks with the "active" state, i.e. highlighted blue if you use the general theme. I'm wondering how I can remove that after click (or tap, etc).

Thanks.

like image 744
Kgosser Avatar asked Sep 21 '11 21:09

Kgosser


3 Answers

You can disable the 'highlighted blue'-state in the 'mobileinit'-event before loading jQueryMobile-script:

<head>
    <script>
    $(document).bind('mobileinit', function () {
        $.mobile.activeBtnClass = 'unused';
    });
    </script>
    <script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
</head>

Now, when you click on a link, no class will be added after the click is performed. You will still have the 'hoover' and 'down' classes.

like image 55
オスカー Avatar answered Oct 18 '22 17:10

オスカー


Update:

This question and the hacks suggested are now a bit outdated. jQuery mobile handles buttons quite a bit differently than 3 years ago and also, jQuery mobile now has several different definitions of "button". If you want to do what the OP was looking for, you might now be able to avoid the issue by using this:

Step 1:

<button class="ui-btn myButton">Button</button>

Alternatively, you could also use jQuery mobile input buttons:

<form>
    <input value="Button One" type="button" class="myButton">
    <input value="Button Two" type="button" class="myButton2">
</form>

Step 2: Then your standard jquery on callback:

$(".myButton").on("tap", function(e) {
    // do your thing
});

If you are using a button or a tab, or whatever, that has the "active" class applied to it (the default is ui-btn-active), the old answer may still be useful to someone. Also, here is a fiddle demonstrating the code below.


Selectively removing active state:

As demonstrated in another answer, you can disable the active state for all buttons on all pages. If that is acceptable for the project in question, that is the appropriate (and simpler) solution. However, if you want to disable the active state for some buttons while preserving active states for others, you can use this method.

Step 1:

    $(document).bind('mobileinit', function() {
        $(document).on('tap', function(e) {
            $('.activeOnce').removeClass($.mobile.activeBtnClass);
        });
    });

Step 2:

Then add the activeOnce class (or whatever you want to call it - it's a custom class) to the buttons that you don't want to highlight when clicking.

And as is usual when binding anything to mobileinit, be sure you place your bindings - and perhaps better, all your javascript code - below the jQuery script and above the jQuery-mobile script.

<script src="js/jquery.js"></script>
<script src="js/my_script.js"></script>
<script src="js/jquery.mobile.js"></script>
like image 27
uɥƃnɐʌuop Avatar answered Oct 18 '22 16:10

uɥƃnɐʌuop


Do NOT set the activeBtnClass to '' as suggested, this will cause errors when closing dialogs and the pageLoading function.

The method described does work, but cannot be set to null, the activeBtnClass variable is used as a selector, so set it to a non-existent class to get the same effect without the error.

<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script type="text/javascript">
        $(document).bind('mobileinit', function () {
            $.mobile.activeBtnClass = 'aBtnSelector';
        });
    </script>
    <script type="text/javascript" src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
</head>

This works well to remove the highlight from the buttons while keeping the active state on other elements.

like image 20
ellemayo Avatar answered Oct 18 '22 18:10

ellemayo