Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing data-icon in jQuery Mobile?

I found a bunch of solutions online, but none of them are working for me. Basically, I want to toggle the icon of a button. Here's the HTML:

  <div data-role="navbar" data-iconpos="top">
    <ul>
      <li><a data-icon="arrow-u">View suggestions</a></li>
    </ul>
  </div>

I tried all of these:

$(this).buttonMarkup({ icon: 'arrow-u' });

//

$(this).attr('data-icon','arrow-u');
$(this).children().children().next().removeClass('ui-icon-arrow-d').addClass('ui-icon-arrow-u');

//

$(this).jqmData('icon','arrow-u');

However, for some reason, the child elements of the button all disappear after any of the above is ran (jQuery Mobile adds a bunch of <span>s inside the button).

like image 574
Leo Jiang Avatar asked Dec 20 '22 18:12

Leo Jiang


2 Answers

The best way to do that is:

$('#button').buttonMarkup({ icon: "home" });
like image 136
Steven Lizarazo Avatar answered Dec 23 '22 09:12

Steven Lizarazo


Try this:

$(this).attr('data-icon','arrow-u').button().trigger("refresh");

See related forum thread: http://forum.jquery.com/topic/how-dynamically-update-data-attributes

like image 30
jeff Avatar answered Dec 23 '22 09:12

jeff