populate plugin - getJSON to populate form

populate plugin - getJSON to populate form

Hi all,

I have a table showing users, each with an edit link like this:
  1. <td>
  2.   <a href="#" id="user1.js" class="edit">edit</a>
  3. </td>
  4. ...
  5. <td>
  6.   <a href="#" id="user2.js" class="edit">edit</a>
  7. </td>
  8. ...
The edit links have an ID which contains the name of the JSON file to load.
This will be replaced later with an url (htp://url?user1) to load the files using getJSON.


Here is an example of the JSON file:
  1. {"userID":"1","userName":"Frank","userFeat1":"","userFeat2"="Feat2","userIcon":"1.gif"}
 
At the end of the page is a form which contains all fields for editing the user settings:
  1. <div id="editForm">
  2.   <form id="editForm">
  3.     <fieldset>
  4.     <legend>Edit User</legend>
  5.     <ul>
  6.       <li>
  7.         <label for="userID">User ID</label>
  8.         <input id="userID" type="text" class="inputtext" value="" maxlength="8" name="userID" />
  9.       </li>
  10.       <li>
  11.         <label for="userName">Name</label>
  12.         <input id="userName" type="text" class="inputtext" value="" maxlength="24" name="userName" />
  13.       </li>
  14.       <li>
  15.         <label for="userFeat1">Feature 1</label>
  16.         <input id="userFeat1" type="checkbox" class="checkbox" value="Feat1" name="userFeat1" />
  17.       </li>
  18.       <li>
  19.         <label for="userFeat2">Feature 2</label>
  20.         <input id="userFeat2" type="checkbox" class="checkbox" value="Feat2" name="userFeat2" />
  21.       </li>
  22.       <li>
  23.         <label for="userIcon">Icon</label>
  24.         <select id="userIcon" class="select-single" name="userIcon">
  25.           <option selected="selected" value="0.gif">No Icon</option>
  26.           <option value="1.gif">Icon 1</option>
  27.           <option value="2.gif">Icon 2</option>
  28.           <option value="3.gif">Icon 3</option>
  29.           <option value="4.gif">Icon 4</option>
  30.           <option value="5.gif">Icon 5</option>
  31.           <option value="6.gif">Icon 6</option>
  32.           <option value="7.gif">Icon 7</option>
  33.           <option value="8.gif">Icon 8</option>
  34.           <option value="9.gif">Icon 9</option>
  35.           <option value="10.gif">Icon 10</option>
  36.         </select>
  37.       </li>
  38.     </ul>
  39.     </fieldset>
  40.   </form>
  41. </div>

Here is the script to detect which JSON file to load:
  1.     jQuery('.edit').click(function() {
  2.         var clickedSUID = jQuery(this).get(0).id;
  3.         readUserSettings(clickedSUID);
  4.     });

and here the function to load the JSON file with $.getJSON and parse the result using the populate plugin:
  1. function readUserSettings(fileName) {
  2.     jQuery.getJSON(fileName, {}, function(data) {
  3.         jQuery('#editForm').populate(data, {debug:1});
  4.     });
  5. }

When I click on an edit link I can see in firebug the correct JSON file is loaded based on the link ID.
Unfortunately, the form is not filled with the loaded JSON values.

Here is the complete file:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5. <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
  6. <script type="text/javascript" src="js/populate.js"></script>
  7. <script type="text/javascript">

  8. var fileName;

  9. jQuery().ready(function() {

  10.     jQuery('.edit').click(function() {
  11.         var clickedSUID = jQuery(this).get(0).id;
  12.         readUserSettings(clickedSUID);
  13.     });
  14. });

  15. function readUserSettings(fileName) {
  16.     jQuery.getJSON(fileName, {}, function(data) {
  17.         jQuery('#editForm').populate(data, {debug:1});
  18.     });

  19. </script>
  20. <style>
  21. ul {
  22.     list-style-type: none;
  23.     margin: 0px;
  24.     padding: 25px;
  25. }
  26. li {
  27.     display: block;
  28.     margin: 0px;
  29.     padding: 5px;
  30. }
  31. li label {
  32.     width: 100px;
  33.     float: left;
  34. }
  35. legend {
  36.     padding-left: 10px;
  37.     font-weight: bold;
  38.     font-variant: small-caps;
  39.     font-size: 16px;
  40. }
  41. body,td,th {
  42.     font-family: Verdana, Arial, Helvetica, sans-serif;
  43.     font-size: 12px;
  44. }
  45. </style>
  46. <title>JSON TESTING</title>
  47. </head>
  48. <body>
  49. <div id="userlist">
  50.   <table>
  51.     <tbody>
  52.       <tr>
  53.         <td>User 1</td>
  54.         <td><a href="#" id="user1.js" class="edit">edit</a></td>
  55.       </tr>
  56.       <tr>
  57.         <td>User 2</td>
  58.         <td><a href="#" id="user2.js" class="edit">edit</a></td>
  59.       </tr>
  60.       <tr>
  61.         <td>User 3</td>
  62.         <td><a href="#" id="user3.js" class="edit">edit</a></td>
  63.       </tr>
  64.       <tr>
  65.         <td>User 4</td>
  66.         <td><a href="#" id="user4.js" class="edit">edit</a></td>
  67.       </tr>
  68.       <tr>
  69.         <td>User 5</td>
  70.         <td><a href="#" id="user5.js" class="edit">edit</a></td>
  71.       </tr>
  72.     </tbody>
  73.   </table>
  74. </div>
  75. <hr />
  76. <div id="editForm">
  77.   <form id="editForm">
  78.     <fieldset>
  79.     <legend>Edit User</legend>
  80.     <ul>
  81.       <li>
  82.         <label for="userID">User ID</label>
  83.         <input id="userID" type="text" class="inputtext" value="" maxlength="8" name="userID" />
  84.       </li>
  85.       <li>
  86.         <label for="userName">Name</label>
  87.         <input id="userName" type="text" class="inputtext" value="" maxlength="24" name="userName" />
  88.       </li>
  89.       <li>
  90.         <label for="userFeat1">Feature 1</label>
  91.         <input id="userFeat1" type="checkbox" class="checkbox" value="Feat1" name="userFeat1" />
  92.       </li>
  93.       <li>
  94.         <label for="userFeat2">Feature 2</label>
  95.         <input id="userFeat2" type="checkbox" class="checkbox" value="Feat2" name="userFeat2" />
  96.       </li>
  97.       <li>
  98.         <label for="userIcon">Icon</label>
  99.         <select id="userIcon" class="select-single" name="userIcon">
  100.           <option selected="selected" value="0.gif">No Icon</option>
  101.           <option value="1.gif">Icon 1</option>
  102.           <option value="2.gif">Icon 2</option>
  103.           <option value="3.gif">Icon 3</option>
  104.           <option value="4.gif">Icon 4</option>
  105.           <option value="5.gif">Icon 5</option>
  106.           <option value="6.gif">Icon 6</option>
  107.           <option value="7.gif">Icon 7</option>
  108.           <option value="8.gif">Icon 8</option>
  109.           <option value="9.gif">Icon 9</option>
  110.           <option value="10.gif">Icon 10</option>
  111.         </select>
  112.       </li>
  113.     </ul>
  114.     </fieldset>
  115.   </form>
  116. </div>
  117. </body>
  118. </html>



Please, can someone help me - what I'm missing here?


Cheers,
Boris