2

I'm very new to Jquery & jquery mobile. I'm resizing a button so it's responsive to window size. To be more specific, i'm changing it from iconpos="left" to iconpos="notext" to remove the text on small windows. I found the following function, which works for me.

$(window).on("throttledresize", function() {
  var smallButtons = $(window).width() <= 480;
  $('#menu_toggle').toggleClass('ui-btn-icon-notext', smallButtons);
  $('#menu_toggle').toggleClass('ui-btn-icon-left', !smallButtons);
});

But it only works on the window resizing. Obviously, I'd also like it showing the correct size on pageload, not just resizing. I found the code below, but I don't know how put them both into 1, more succinct bit of code.

$("#page_id").on("pageshow" , function() {
 The Function
});
devsie
  • 131
  • 11

1 Answers1

2

jQuery Mobile >= 1.4

.buttonMarkup() as well as data-role="button" are deprecated and will be removed in 1.5. Instead, classes should be added manually to Anchor tag.

  • Create a function to manipulate classes of Anchor. As of jQM page events are now replaced with pageContainer events. The new events can't be bound to a specific page, therefore, you need to look for the Anchor inside active page.

    Note that $.mobile.activePage is also deprecated and replaced with $.mobile.pageContainer.pagecontainer("getActivePage").

    function resizeBtn() {
        var activePage = $.mobile.pageContainer.pagecontainer("getActivePage");
        if ($(window).width() <= 480) {
            $("#notext.ui-btn-icon-left", activePage)
                .toggleClass("ui-btn-icon-notext ui-btn-icon-left");
        } else {
            $("#notext.ui-btn-icon-notext", activePage)
                .toggleClass("ui-btn-icon-left ui-btn-icon-notext");
        }
    }
    
  • Call function on pagecontainerbeforeshow event:

    $(document).on("pagecontainerbeforeshow", resizeBtn);
    
  • Call function on throttledresize event:

    $(window).on("throttledresize", resizeBtn);
    

Note: throttledresize is better than resize as it delays firing resize event coming from the browser.

Demo


jQuery Mobile <= 1.3

You need to use .buttonMarkup() if you're using jQuery Mobile 1.3 or lower.

$(".selector").buttonMarkup({
  iconpos: "notext"
});
  • Resize function:

    function resizeBtn() {
        if ($(window).width() <= 480) {
            $(".selector").buttonMarkup({
                iconpos: "notext"
            });
        } else {
            $(".selector").buttonMarkup({
                iconpos: "right"
            });
        }
    }
    
  • Call function on pagebeforeshow:

    $(document).on("pagebeforeshow", resizeBtn);
    
  • Call function on resize:

    $(window).on("resize", resizeBtn);
    

Demo

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Omar
  • 32,302
  • 9
  • 69
  • 112
  • thanks, I'm trying out your reply atm. Looks good. So jquery1.5 is coming out? I'm only just starting to get the idea of 1.4. Thanks for the reply. – devsie Feb 12 '14 at 23:34
  • @devsie You're welcome. I've made a minor change `pagecontainerbeforeshow`. I dont think it will come out soon, `.buttonMarkup()` should work normally, but to be on the safe side, considered removed and stick to working functions :) – Omar Feb 12 '14 at 23:35
  • Is it also better to use resize than throttled resize? And if doing things with class="ui-btn" works at the moment and is the way forward, then I'll do things that way, rather than having to change them again in the future. – devsie Feb 12 '14 at 23:38
  • @devsie `throttledresize` is better. you can also use `orientationchange` with `throttledresize`. – Omar Feb 12 '14 at 23:39