List loosing style + can't get access to variable set in function

List loosing style + can't get access to variable set in function

Hi guys.
I'm having two problems right now. I'm trying to write a page that takes out a number of items from a Web SQL and creates a list. Should be easy. The WQeb SQl is working fine (i can see the 4 rows in Chrome Web dev).

This is the javascript that I am adding at the end of the page (as done by a nettuts+ tutorial):

  1. <script>
  2. // Variables
  3. var html_sub_string;
  4. var html_string;

  5. $(document).ready(function () {     
  6.      showList();
  7.      
  8. });    

  9. function showList(){  
  10. var db = openDatabase('LaurDB', '1.0', 'LaurDB', 2 * 1024 * 1024);
  11. db.transaction(function (tx) {
  12. tx.executeSql('SELECT * FROM laureates', [], function (tx, results) {
  13.  var len = results.rows.length, i;
  14.  for (i = 0; i < len; i++) {
  15. html_sub_string = html_sub_string + '<li>' + results.rows.item(i).category + ' ' + results.rows.item(i).year + '<br>' + results.rows.item(i).name + '</li>';
  16.  }  
  17. });
  18. });
  19. $('<li data-role="list-divider">Results</li>' +  html_sub_string + ' <li data-role="list-divider">End Results</li>').appendTo('#listan'); 
  20. }

  21. </script>
However, I have 2 problems with this:

1/ I can't seem to get the vaule of html_sub_string out from the function that loops through the database. I thought that by defining it a Global variable it would be accessable from all functions? What am I doing wrong here?

2/ If I put this code at the bottom of the page the resulting list is halfway styled. It has rounded corners and has the "inset"-look but all colors are gone and the text is unstyled. 

If I instead put the code in a separate js-file and references it before jquery.mobile-1.0a3.min.js it actually styles correctly. 

<script src="jquery-1.5.min.js"></script>
<script src="result.js"></script>
<script src="jquery.mobile-1.0a3.min.js"></script>

While this list is being created on a separate page now I ultimately would like it to be created on the fly on the index-page through a onclick-even and when I tried it I got an unstyled list so that's why I still am wondering what is happening.

Thanks

/Björn

    • Topic Participants

    • admin