I found this case to be so weird, that I had to look deeper. I have copied your code across and staged it on my own server to see what was going on.
Well the solution came to me as I was reading the wiki notes at the bottom of the jQuery documentation. They hint that events are not copied across during an append operation. This is not mentioned within the main text of the documentation though.
So, with a bit of testing, I can confirm... you cannot (and should not) append events.
You should, arguably in any case, instead add a delegated event handler afterwards. This is how I have re-written TaskLists.js (I have not tested the local storage aspect)...
- $('#taskPage').on('pageshow', function(event) {
- var id = localStorage.getItem('projectID');
- var serviceURL = "http://******";
-
- $.getJSON(serviceURL + 'getTaskLists.php?id=' + id, function(json){
- var results = json.results;
- var $taskList = $('#taskList'); //copy, rather than re-calculate repeatedly
- //validate not error returned instead of results
- if (!results){ //shortcut test for 0, null, undefined, false, NaN, empty string
- $taskList.append('<li>You have no task lists</li>');
- $taskList.listview('refresh');
- }
- //grab task Lists
- $.each(results, function(i,taskLists){
- $taskList.append('<li><a href="projectTaskDetails.html" data-id="' + taskLists.id + '"><h3>' + taskLists.name + '</h3><p></p><p>' + taskLists.description + '</p><p>Start Date: ' + taskLists.start_date + '</p><p>Due Date: ' + taskLists.due_date + '</p></a></li>');
- });
- $taskList.on('click', 'a', storePage);
- $taskList.listview('refresh');
- });//<--end post
- });
- function storePage(){
- var pageID = $(this).data('id'); //"this" and "$(this)" represent the clicked <a>
- console.info('inside onClick taskLists function: ' + pageID);
- //localStorage.setItem('tlID',pageID);
- return true; //or whatever you wish to return as <a> onclick result
- }
The key changes are:-
- Swapped your .live() for a .on(). If you are running version of jQuery older than v1.7, then you should stick with .live().
- Used a cached selector, var $taskList = $('#taskList'); , so jQuery does not need to recalculate the reference every time it's used (a lot of times).
- Within the .append(), save the pageID value into a custom Data attribute, then read it out later, as required.
- Add a jQuery delegated event handler for the newly appended DOM fragments (if your jQuery is older than v1.7, use .live() instead of the newer .on().
I strongly suggest you change the "project" JavaScript too. It's almost certainly a pure fluke it worked in your test cases. Shout if you need assistance.
Best wishes,
Alan