3

I am working on a JQuery Mobile app. I am dynamically adding HTML when the page loads. A trimmed down example is shown here:

var h = "";
for (i=1; i<=5; i++) {
  h += "<div>Entry #" + i + "</div>";
  h += "<div class='ui-grid-a'>";
    h += "<div class='ui-block-a'><input type='button' value='Approve' onclick='return approveButton_Click(this);' /></div>";
    h += "<div class='ui-block-b'><input type='button' value='Reject' onclick='return rejectAbuse_Click(this);' /></div>";
  h += "</div><hr />";
});
$("#entries", "#myPage").append(h);

My HTML is appearing in the UI. However, the buttons are not rendered as a typical JQuery Mobile buttons. Instead, they look like traditional HTML buttons. How do I get my dynamically added buttons to apply the JQuery mobile styling?

Thank you

user70192
  • 13,786
  • 51
  • 160
  • 240

2 Answers2

7

Here's an working example out of your code: http://jsfiddle.net/Gajotres/NuCs2/

Before you can refresh the button/s it first must be initialized with .button() function. Just like this:

$("#approve"+i).button().button('refresh');
$("#reject"+i).button().button('refresh');

There's also another solution but you should use it only if you are recreating a whole page:

$("#index").trigger("pagecreate");

And here's an example for the second solution: http://jsfiddle.net/Gajotres/mpFJn/

If you want to find out more about methods of markup enhancement take a look at my other ARTICLE, let me be transparent, it is my personal blog. Or find it HERE.

Community
  • 1
  • 1
Gajotres
  • 57,309
  • 16
  • 102
  • 130
  • 2
    Is there a reason to both initialize and refresh the widget? Since the HTML has just been added to the DOM there shouldn't be a need to refresh anything. Or am I off base for some reason? Also, your first JSFiddle is displaying a 404 error. – Jasper Feb 13 '13 at 22:20
0

Use "Refresh" method in Jquery mobile for dynamic appearance of Jqm UI...

http://jquerymobile.com/demos/1.2.0/docs/buttons/buttons-methods.html

SaurabhLP
  • 3,619
  • 9
  • 40
  • 70
  • 1
    When I try to do that, I get an error that says: Uncaught cannot call methods on button prior to initialization; attempted to call method 'refresh' – user70192 Jan 12 '13 at 15:14