invisible invisible -4 years ago 80
TypeScript Question

When i create list manually in javascript, cant get the values using querySelectorAll();

let List = document.createElement("div");
List.setAttribute('id', 'test');
let List1 = document.createElement("ul");
let List2 = document.createElement("li");
let Value = document.createTextNode("java");
List2.appendChild(Value);
let List3 = document.createElement("li");
Value = document.createTextNode("ajax");
List3.appendChild(Value);
List1.appendChild(List2);
List1.appendChild(List3);
List.appendChild(List1);



When i enter let li = document.querySelectorAll("#test li");


I didnt get list element with li tag?? whats wrong with the code?

Answer Source

You need to append the List node that you have created to the document. Try this:

let List = document.createElement("div");
List.setAttribute('id', 'test');
let List1 = document.createElement("ul");
let List2 = document.createElement("li");
let  Value = document.createTextNode("java");
List2.appendChild(Value);
let List3 = document.createElement("li");
Value = document.createTextNode("ajax");
List3.appendChild(Value);
List1.appendChild(List2);
List1.appendChild(List3);
List.appendChild(List1);

document.body.appendChild(List);

Now your document.querySelectorAll statement should work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download