I am new to Javascript I am having a tonne of problems with the program syntax please what's wrong with the program below? I am trying to create a function that takes an argument of an object in the two arrays. These objects does not exist in the DOM but I want to change color when clicked.
let availableLibraries = [
{ id: 1, name: 'express' },
{ id: 2, name: 'async' },
{ id: 3, name: 'request' },
{ id: 4, name: 'browserify' },
{ id: 5, name: 'grunt' },
];
let librariesForInstallation = [
{ id: 6, name: 'socket.io' },
{ id: 7, name: 'mocha' },
];
let selectedLibrary = null;
const selectLibrary = function (library) {
// TODO: implement
let newDiv=document.createElement('div');
let newContent=document.createTextNode(library);
document.newDiv.appendChild(newContent);
newDiv.setAttribute('id', 'selected');
document.getElementById('selected').style.backgroundColor='pink';
document.getElementById('selected').style.borderBottomColor='red';
selectedLibrary = {
id:library.id, name:library.name};
Maybe this is what you had in mind?
const availableLibraries = [
{ id: 1, name: 'express' },
{ id: 2, name: 'async' },
{ id: 3, name: 'request' },
{ id: 4, name: 'browserify' },
{ id: 5, name: 'grunt' },
], librariesForInstallation = [
{ id: 6, name: 'socket.io' },
{ id: 7, name: 'mocha' },
], cnt=document.getElementById("content");
cnt.innerHTML=availableLibraries.concat(librariesForInstallation)
.map(l=>`<div id="lib${l.id}" class="lib">id:${l.id}, name:${l.name}</div>`).join("\n");
cnt.onclick=ev=>{
const tcl=ev.target.classList;
if(tcl.contains("lib")) tcl.toggle("selected");
}
.selected {background-color:pink;border-width:0.3em; border-style:solid; border-bottom-color: red;}
<div id="content"></div>