Skip to content

Magento 2 How to get td value using script and assign that value to php variable in same phml file?

I have php variable called itemId in same phtml file and i got td value using js. now i need to assign that var id value to $itemId variable.

<?php

$itemId = null;

?>



        <table class="item-table">
        
            <tr data-row="product-item" id="<?= $block->escapeHtmlAttr($item->getId()) ?>" onclick="getTableRowData(this)">
              
                <td class="item-id" id="item-id">
                    <span class="item-id-span">
                        <?php echo $item->getId(); ?>
                    </span>
                </td
            </tr>
    </table>


<script>

function getTableRowData(row) {
var cells = row.getElementsByTagName('td');
        var id = cells[1].innerHTML;
}

</script>