Solace Solace - 11 days ago 5
HTML Question

Passing number from HTML to JS function, becomes "undefined" in JS. Why?

Here is the PHP and HTML:

<?php $rowCountInDataTable=600; ?>
...
<button onclick="<?php echo "showNextRows($rowCountInDataTable)"; ?>"></button>;


Here is the JS:

function showNextRows(totalRowCount) {
alert("button clicked");//check
alert("Total row count: " . totalRowCount);//check
console.log("Total row count: " . totalRowCount);//check
...
}


Both
alert
and
console.log
print
undefined
. The question is why? and what can I do to get the correct value in JS, which is
600
.

Answer

Unlike PHP, in JavaSript the concatenation operator is +, not ., as you have written.

Change dots to pluses. i.e.:

alert("Total row count: " + totalRowCount); //check