Change:
onClick="View(<?php echo $id; ?>)"
to
onClick="View('<?php echo $id; ?>')"
JavaScript is interpreting the value as a number so you need to tell it to use the value as a string by enclosing it in quotes.
Вопрос
I'm trying to pass a number with zeroes (cuz is form database) to a javascript function:
<table>
<tr onClick="View(<?php echo $id; ?>)">
<td> <?php echo $id; ?> </td>
</tr>
</table>
Javascript function:
function View(id){
alert(id);
}
For exm.:
If my id is 0005
, alert 5
.
If my id is 0006
, alert 6
.
I need to alert '0005
' - '0006
' but i can't solve this.
Thank you for answers
Решение
Change:
onClick="View(<?php echo $id; ?>)"
to
onClick="View('<?php echo $id; ?>')"
JavaScript is interpreting the value as a number so you need to tell it to use the value as a string by enclosing it in quotes.