0

I am trying to set some html in JQuery Mobile. It does add the html but it loses the CSS when the html is added to the div. The code is like this:

<div data-role="page" id="test">
  <div>
    <a id="testButton" data-role="button" data-inline="true">Hit me</a>
  </div>
  <div id="testDiv" data-role="content">
  </div>
</div>

and Javascript to add some html in the "testDiv" like this:

$(document).ready(function()
{
  $('#testButton').click(function() {
    var output = '<ul data-role="listview">';
    output += '<li><a href="#">Contact A</a></li>';
    output += '<li><a href="#">Contact B</a></li>';
    output += '<li><a href="#">Contact C</a></li>';
    output += '</ul>';

    $('#testDiv').html(output);
  });

});

Any help will be appreciated.

Thanks

2 Answers 2

2

You need to tell jQuery Mobile that the newly inserted HTML is actually a listview by calling listview():

$('#testButton').click(function() {
    var output = '<ul data-role="listview" class="newlistview">';
    output += '<li><a href="#">Contact A</a></li>';
    output += '<li><a href="#">Contact B</a></li>';
    output += '<li><a href="#">Contact C</a></li>';
    output += '</ul>';

    $('#testDiv').html(output);
    $('.newlistview').listview().removeClass("newlistview")
  });
Sign up to request clarification or add additional context in comments.

Comments

0
$('#testDiv').html(output).trigger("create");
$('.newlistview').listview("refresh");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.