Home » Jquery » javascript – Getting a child by using its id which build at runtime-Exceptionshub

javascript – Getting a child by using its id which build at runtime-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have a table and its tbody is adding rows at runtime with icons(delete and update),
Now I want when I click on delete icon, I can get the innerHTML by using the id not an index.
I can use this to get the innerHTML by using index
event.target.parentNode.parentElement.parentElement.children[0].innerHTML;

but what I want is to get the child by using it child id which created at runtime not in HTML and build before??

Can anyone help me with this.
Thanks in advance.

How to&Answer:

It is so simple;
assign a unique ID to the HTML element and then grab it with the document.getElementById(‘id’).innerHTML

Here is an example below.

<!DOCTYPE html>
<html>
<body>

<p id="demo">Click the button to change the color of this paragraph.</p>

<button onclick="myFunction()"><p>Try it</p></button>

<script>
function myFunction() {
  var x = document.getElementById("demo").innerHTML;
    console.log(x);
}
</script>

</body>
</html>

For further assistance please visit
https://www.w3schools.com/jsref/met_document_getelementbyid.asp