Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't really need to use an event. When you get the updated list, call the update method directly.</p> <p>However, as other users have indicated, using a data-binding library might be better.</p> <p>start_with.html</p> <pre><code>&lt;div id="users"&gt; &lt;ul class="list"&gt; &lt;li&gt; &lt;h3 class="name"&gt;name1&lt;/h3&gt; &lt;/li&gt; &lt;li&gt; &lt;h3 class="name"&gt;name2&lt;/h3&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre> <p>update_list.js</p> <pre><code>// Note: This method will clear any user selections and may cause other problems since the previous list is deleted and recreated. function update_list(updated_users) { // clear the existing list $('#users .list li').remove(); $.each(updatedUsers, function(index,userName) { $('#users .list').append('&lt;li&gt;&lt;h3 class="name"&gt;'+userName+'&lt;/h3&gt;&lt;/li&gt;') }); } </code></pre> <p>example_1.js</p> <pre><code>// Get the updated list var updated_users = ["name1", "name2", "name3"]; // Update it update_list(updated_users); </code></pre> <p>after_example_1.html</p> <pre><code>&lt;div id="users"&gt; &lt;ul class="list"&gt; &lt;li&gt;&lt;h3 class="name"&gt;name1&lt;/h3&gt;&lt;/li&gt; &lt;li&gt;&lt;h3 class="name"&gt;name2&lt;/h3&gt;&lt;/li&gt; &lt;li&gt;&lt;h3 class="name"&gt;name3&lt;/h3&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre> <p>example_2.js</p> <pre><code>// Example by AJAX get using a made-up URL $.get('http://api.example.com/users/active.json', function(data) { // What you do with data depends on what the API returns... // I'm assuming it returns an array of users. // data = ['user1', 'user2', 'user3']; update_list(data); }) </code></pre> <p>after_example_2.html</p> <pre><code>&lt;div id="users"&gt; &lt;ul class="list"&gt; &lt;li&gt;&lt;h3 class="name"&gt;user1&lt;/h3&gt;&lt;/li&gt; &lt;li&gt;&lt;h3 class="name"&gt;user2&lt;/h3&gt;&lt;/li&gt; &lt;li&gt;&lt;h3 class="name"&gt;user3&lt;/h3&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre> <p>Note: One drawback to this method is that the old list gets destroyed. This means that if your list has input boxes, for example, their user-entered content would be destroyed on update. If you have state in your list, you'll have to use a different method to update the list if you also want to preserve the state of elements.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload