I have a div which contain other tags inside it
<div id="mainDiv">
<div>
<table>
<tbody>
<tr>
<td>1</td>
<td>item</td>
<td>item</td>
<td>2</td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td>1</td>
<td>item</td>
<td>item</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
</div>
How can I access <td>
of this mainDiv through javascript. I want to change innerHTML of these <td>
Use the textContent property to get the text of a div element, e.g. const result = element. textContent . The textContent property will return the text content of the div and its descendants. If the element is empty, an empty string is returned.
To select a specific HTML class using JavaScript, we need to target it and then store it as a variable. Here is the one line of JavaScript we need to target this element and store it as a variable: Code from a text editor: const vanillaDescription = document. querySelector('.
In this article, we will find how to get all elements of a specific class inside an HTML div tag. To do this we will use HTML DOM querySelectorAll()method. This method of HTML DOM (document object model) returns all elements in the document that matches a specified CSS selector(s).
You can add <script></script> inside a DIV tag. Just check on w3c, it is valid HTML.
var allDivTd = document.getElementById("mainDiv").getElementsByTagName("TD");
for(var i = 0; i < allDivTd.length; i++){
var td = allDivTd[i];
td.innerHTML = // do something w/ inner html
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With