Gil Gil - 11 months ago 75
Ajax Question

using jQuery mobile with ajax

I am trying to print a jQuery mobile thing using ajax but it doesn't encode the result as jQuery mobile is supposed to do.

This is a simplified version of the part of the javascript code that is supposed to do so:

<script type="text/javascript">
function changePage(task) {
var objText = "";
type: "POST",
url: "DataFetch.aspx/FetchData",
data: '{id: ' + <%=Session["loggedID"] %> + ', task: ' + task + ' }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var obj = JSON.parse(response.d);
if (task == 2)
objText += "<div data-role='collapsible'><h3>click me</h3><p>text</p></div>";
document.getElementById('content' + task).innerHTML = objText;



How can I make it work? (when I write it explicitly in the html or outside the ajax functions it works but I need it to work with json)

Answer Source

Try changing

document.getElementById('content' + task).innerHTML = objText;


$('#content' + task).HTML(objText).enhanceWithin();

The enhanceWithin() call tells jQM to enhace the new content you added via AJAX: