Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's some tips to, at least, make your code more portable (dunno if it will be better performance wise, but should be, since DOM Manipulation is less expensive)</p> <h2>Tips</h2> <ol> <li>First separate your event handle from the HTML</li> <li>Pass the "new food" as a function paramater</li> <li>Tie the array elements to the DOM using the ID</li> <li>Instead of rerendering everything when something changes (using innerHTML in the list), just change the relevant bit</li> </ol> <h2>Benefits:</h2> <ol> <li>You actually only loop once (when removing elements from the array).</li> <li>You don't re-render the list everytime something changes, just the element clicked</li> <li>Added bonus: It's more portable.</li> <li>Should be faster</li> </ol> <h2>Example code:</h2> <p><a href="http://jsfiddle.net/tivie/WX7ZW/4/" rel="nofollow"><strong>FIDDLE</strong></a></p> <h3>HTML</h3> <pre><code>&lt;div id="eventBinder"&gt; &lt;!-- we add to our foodList from the value of the following input --&gt; &lt;input id="addFood" type="text" value="food" /&gt; &lt;!-- we call addToFood(); through the following button --&gt; &lt;button id="addFoodBtn" value="Add more to food"&gt;Add Food&lt;/button&gt; &lt;!-- The list of food is displayed in the following div --&gt; &lt;div id="foods"&gt;&lt;/div&gt; &lt;/div&gt; </code></pre> <h3>JS</h3> <pre><code>// FoodList Class var FoodList = function (selectorID) { return { foodArray: [], listEl: document.getElementById(selectorID), idCnt: 0, add: function (newFood) { var id = 'myfood-' + this.idCnt; this.foodArray.push({ id: id, food: newFood }); var foodDom = document.createElement('div'), foodText = document.createTextNode(newFood); foodDom.setAttribute('id', id); foodDom.setAttribute('class', 'aFood'); foodDom.appendChild(foodText); this.listEl.appendChild(foodDom); ++this.idCnt; }, remove: function (foodID) { for (var f in this.foodArray) { if (this.foodArray[f].id === foodID) { delete this.foodArray[f]; var delFood = document.getElementById(foodID); this.listEl.removeChild(delFood); } } } }; }; //Actual app window.myFoodList = new FoodList('foods'); document.getElementById('eventBinder').addEventListener('click', function (e) { if (e.target.id === 'addFoodBtn') { var food = document.getElementById('addFood').value; window.myFoodList.add(food); } else if (e.target.className === 'aFood') { window.myFoodList.remove(e.target.id); } }, false); </code></pre>
    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.
    1. VO
      singulars
      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