Thursday, 5 September 2013

jquery mobile - how to connect between and the click event?

jquery mobile - how to connect between and the click event?

I'm a newbie to jqeury and jquery mobile.
I followed a few samples and documentations and created the following code:
function initListByUrl(id, requestUrl) {
$.ajax({
url: requestUrl,
type:'GET',
dataType:'json',
success: function(data) {
var items = []
$.each(data, function(i, elem) {
items.push('<li id="cat_li_'+i+'"><a href="#places">' +
elem.name + '</a></li>');
});
id.html(items).listview('refresh');
}
});
}
$(document).on("click", '[id^=cat_li]', function(event, ui) {
console.log(3);
})
I need to have the elem event from the $.ajax available in the context of
the click event
This is because the elem object holds a field that is relevent when the
item is clicked.
How should I do it?
EDIT
just to clarify.
the initListByUrl functions is called in document-ready. It builds a list
view by the retrieved json object which contains an array.
if I had elem in the context of the on-click event I would have done
something like this:
$(document).on("click", '[id^=cat_li]', function(event, ui) {
call_some_function(elem.someField);
})
My issue is how do I have the appropriate elem object in the scope of the
on-click event?

No comments:

Post a Comment