I’m trying to find the value of elements that are generated by code.
This makes the elements:
let numNotifs = 1;
// this below is inside a loop
addFill = `<br><input type = 'number' id = 'notif${numNotifs}'>';
++numNotifs;
And I try to find them using this:
let i = 1;
while (i < numNotifs + 1) {
save(parseInt(document.querySelector(`#notif${i}`).value));
++i;
}
However it gives me Uncaught TypeError: document.querySelector(...) is null
even though this exists in the html file.
<input type="number" id="notif1" min="1" max="60">
How should I fix this?
2
Answers
I made sample code as per your description. If it works for you and you need more information on how it works I will write a more detailed description.
Example:
Viewing your code led me this conclusion that you want to create some elements through loops and then want to access those !
Here’s an exmaple also note instead of changing the innerHTML which is pre assumable since you’re writing some html string and it is not what you want to consider, you must create elements using Javascript.
Note I used defer attribute in script tag which ensures that the script is downloaded in parallel to parsing the page, and executed after the page has finished parsing. ( no need to listen to dom load event )