Need Help Fast


#1

Does anyone know how to write some JavaScript code the will find out how to set the text property of the list items and them to append the entry to the list in my HTML file?

This is the code I have written so far and the new line of code needs to go after var entry = document.createElement(‘li’); in the function displayAlbum

function init() {
‘use strict’;

var searchVal = localStorage.getitem(“searchVal”);
for(var i = 0; i < dbArray.length; i++) {
var album = dbArray[i];
if (album.artist == searchVal)
displayAlbum(album);
}
} // End of init() function.

function displayAlbum(album) {
var list = document.getElementById(‘results’);
var entry = document.createElement(‘li’);
//find out how to set the text prop of the list items
//append entry to the list
}
// Assign an event listener to the window’s load event:
window.onload = init;

I need to turn this in today so a quick reply would be greatful.


#2

Can you share some of the approaches you’ve tried?

The concepts in this tutorial will get you most of the way there: https://www.kirupa.com/html5/creating_dom_elements_and_other_stuff.htm


#3

Honestly I’m totally new to js coding but this code is for one of my assignments and I am lost as to how to solve the problem. My teacher seems to think we students are pros at writing js like he is and doesn’t help us much. I have your book and I have been reading it when I have the time and it’s been very helpful but not to where I understand the problems my teacher gives us.


#4

The thing is that I am pretty certain you’ve almost figured the answer out. Quickly read through the “Creating Elements” section in the article I linked you to :slight_smile:

If I understand your code correctly:

  • displayAlbum will be called for each entry in dbArray

  • The list variable stores a reference to your parent DOM element

  • For each album, you want to create a new list item and add it to the DOM element referenced by list.

  • With the entry variable, you are on the right track. You created the new DOM element. What you need to do is append this element to the DOM element referenced by list.

  • For setting the text property of a list element, look into the setAttribute method here: https://www.kirupa.com/html5/modifying_dom_elements.htm

Let me know if this gets you going on the right path. I believe you need at most about 5 lines of additional code beyond what you already have!

Cheers,
Kirupa