Search code examples
javascriptonkeyup

How to set dynamic values with onKeyUp javascript?


How can I set dynamic values for function onKeyUp? Right now I'm hardcoding the number but I need to retrieve it from a php variable.

php value

$num = $queryResults['col_number'];

html

<input onKeyUp="if(this.value>'$num'){this.value='$num';}
else if(this.value<0){this.value='0';}" />

Solution

  • You need to output the value using <?php echo $num ?> or shorter <?= $num ?>

    <input onKeyUp="if(this.value>'<?= $num ?>'){this.value='<?= $num ?>';} else if(this.value<0){this.value='0';}" />