4a.17. Completing addToTop5()Here's the completed addToTop5() function. Make sure your answers match ours, and then add all of this new code to top5.js. function addToTop5() { var imgElement = this; var top5Element = document.getElementById("top5"); var numCDs = 0; for (var i=0; i<top5Element.childNodes.length; i++) { if (top5Element.childNodes[i].nodeName.toLowerCase() == "img") { numCDs = numCDs + 1; } } if (numCDs > Compare numCDs to 5 to prevent too many CDs from being added.= 5) { alert("You already have 5 CDs. Click \"Start Over\" to try again."); return; } top5Element.appendChild(imgElement); imgElement.onclick = null; var newSpanElement = document.createElementAll the create methods are available through the document object.("span"); newSpanElement This sets the "className " attribute of the <span>..className = "rank"; var newTextElement = document.createTextNode (numCDs The text node should have the next CD ranking.+ 1); newSpanElement.appendChild(newTextElement); Just stick the text node at the end of <span>'s child nodes list. top5Element.insertBefore(newSpanElementThe <span> goes before this CD's <img> element, under the "top5" <div>., imgElement); } |