Later on t...">

Document.getElementById returns [HTMLTableCellElement object]

I now have a td:

<td id="setCustomer"><?= $customer ?></td>

      

Later on the page I have:

<script>
document.getElementById('setCustomer').onclick = function(){
var newOne = document.getElementById('setCustomer');
console.log("var newOne is "+newOne); 
}
</script>

      

When I click on a table cell that contains this customer name, the function fires up and spits out:

var newOne is [object HTMLTableCellElement]

      

instead:

var newOne is ACME CORP.

      

what am I doing wrong?

+3


source to share


1 answer


You want the content of the element, not the element itself:



var newOne = document.getElementById('setCustomer').innerHTML;

      

+11


source







All Articles