1

Possible Duplicate:
jQuery Mobile does not apply styles after dynamically adding content

I'm wondering if I am missing something here...

If I write

<a onclick="deleteThis()" data-role="button" data-theme="a">Button text</a>

within the body of an HTML page it works fine. But when I programatically try and add new buttons using the following :

$('.block').append('<a onclick="deleteThis()" data-role="button" data-theme="a">' + results[i].Title + '</a>');

The link is appended fine just with no JQuery mobile styling, data-theme="a" should apply a particular colour swatch css to the element in question.

Where am I going wrong?

Community
  • 1
  • 1
user1202278
  • 774
  • 2
  • 10
  • 24

3 Answers3

5
 $('.block').append(' <a onclick="deleteThis()" data-role="button" data-theme="a"> Text</a>').trigger( "create" );
user1202278
  • 774
  • 2
  • 10
  • 24
0

You can use the .buttonMarkup() method supplied by jQuery Mobile. This method allows you to pass some optional parameters as well.

Here are the docs for the .buttonMarkup() method: http://jquerymobile.com/demos/1.1.0/docs/buttons/buttons-options.html

Here is an example of using this method to create a button:

$('<a href="#" />').text('New Button').buttonMarkup({
    theme  : 'a',
    icon   : 'star',
    mini   : false,
    inline : false
});
//these are just a few of the options

Here is a demo: http://jsfiddle.net/jasper/6BF6M/

Also, might I suggest using jQuery to make your event bindings, something like:

$('.block').append(
    $('<a href="#" />').text('New Button').buttonMarkup().bind('click', deleteThis)
);
Jasper
  • 75,717
  • 14
  • 151
  • 146
-1

try triggering the refresh event, for a page:

$('#pageId').trigger('pagecreate');

if it's a listview:

$('#listId').listview('refresh');
Th0rndike
  • 3,406
  • 3
  • 22
  • 42
  • Thanks, I've tried that with no success unfortunately – user1202278 Jul 10 '12 at 15:10
  • what's the structure of your html? – Th0rndike Jul 10 '12 at 15:10
  • I've got a simple mobile layout on the go. Onload of the page I have javascript querying an external api to retrieve some information to then display in a series of links styled like buttons. The data retrieval from the api works fine and is appended fine just with no styling. – user1202278 Jul 10 '12 at 15:14
  • @user1202278 are you sure? i just tried this, and theme was applied to the dinamically created button. Just call ('pagecreate') after the append – Th0rndike Jul 10 '12 at 15:28
  • Yup, but calling .trigger( "create" ) after the append worked fine – user1202278 Jul 10 '12 at 15:29