I am working on an MVC asp.net project and within the browser debugger console I get this error. However, the innerText is read because it is pasting the text to a div in my project.
This is the javascript
function addtoList(clicked_id) {
var ProblemLabel = document.getElementById(clicked_id).innerText;
//alert(clicked_id);
document.getElementById("ProblemSelectionMade").textContent = `${ProblemLabel}`;
}
My question is this, why would I get this error when the value clearly isn't null and it is being read?
The reason I need to fix it is because it interrupts other functions from being executed.
EDIT: This is the full error that is given
Create:443 Uncaught TypeError: Cannot read property 'innerHTML' of null at addtoList (Create:443) at CardButtonClick (Create:463) at HTMLDivElement.onclick (Create:1)
Where HTMLDivElement.onclick is a call to CardButtonClick, and where CardButtonClick contains multiple functions that are being ran.
I have tried doing research and I think I may know what the problem is. The clicked_id, is being dynamically created. I have checked within the browser and it does have an id. But, I am wondering if since it is dynamically created it is not added to the DOM and I need to do that somehow.
EDIT AGAIN: I have done more research and am very sure that the error occurs due to being dynamically created. This is how the element is created in this js
success: function (result) {
// alert(result);
var s = '<div></div>';
for (var i = 0; i < result.length; i++) {
s += '<div class="container"><div class="card"><div class="card-body" onclick="ChangeColors(this.id); CardButtonClick()" id="' + result[i].value + '">' + result[i].name + '</div></div ></div>';
}
// alert(s);
$('#ProblemCards').html(s);
},
And this is an example of the created element
<div class="card-body" onclick="ChangeColors(this.id); CardButtonClick()" id="12492">2 Thermocouples Mixer Body Temperature</div>
So, clicked_id is equivalent to result[i].value.
What I need to do, and don't know how is use an insertBefore or appendChild I believe.