Filipe Ferminiano Filipe Ferminiano -4 years ago 98
HTML Question

Access table cell with javascript in chrome console

I have this table and I want to access the prod_sku value and define it as a variable:

<tr class="border first last" id="order-item-row-386470">
<td><h3 class="product-name">prod_name</h3>
</td>
<td data-rwd-label="SKU">prod_sku</td>
<td class="a-right" data-rwd-label="Preço">
<span class="price-excl-tax">
<span class="cart-price">

<span class="price">prod_price</span>
</span>


</span>
<br>
</td>
</tr>


I get this table with this line:

data = document.getElementsByClassName("odd")[0].innerHTML;


From this tbody

<tbody class="odd">
<tr class="border first last" id="order-item-row-386470">
<td><h3 class="product-name">prod_name</h3>
</td>
<td data-rwd-label="SKU">prod_sku</td>
<td class="a-right" data-rwd-label="Preço">
<span class="price-excl-tax">
<span class="cart-price">
<span class="price">prod_price</span>
</span>


</span>
<br>
</td>
<td class="a-right" data-rwd-label="Qtd">
<span class="nobr">
Solicitado: <strong>1</strong><br>
</span>
</td>
<td class="a-right last" data-rwd-label="Subtotal">
<span class="price-excl-tax">
<span class="cart-price">

<span class="price">prod_price</span>
</span>


</span>
<br>
</td>
</tr>
</tbody>


How can I access prod_sku?

Answer Source

You can use a query selector to match the data-rwd-label attribute.

document.querySelector('td[data-rwd-label=SKU]').innerHTML
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download