Variable passed from php to javascript does not show all characters

0

I have this function on a button

"<td><button type='button' class='btn btn-primary' onclick='CrudNotas(".$row['rut_alumno'].")'><span class='glyphicon glyphicon-plus' aria-hidden='true'></button></td>"

which when I check the button, this appears to me

    onclick="CrudNotas(12664999-8)"

later when I receive the variable in javascript I do it like this

    function CrudNotas(str) {
    //resto del código
    }

but when I print the variable str, it shows me this 12664991.

The truth that I assume should be the way I receive the variable in javascript, but I do not know for sure, anyway, thanks in advance.

    
asked by alexi gallegos 06.12.2017 в 00:44
source

1 answer

2

Try forcing it to pass as String :

"<td><button type='button' class='btn btn-primary' onclick='CrudNotas(\"".$row['rut_alumno']."\")'><span class='glyphicon glyphicon-plus' aria-hidden='true'></button></td>"

Apparently what happens is that he receives the parameter as Number s, interprets the hyphen (-) as an arithmetic operator of subtraction and performs the operation:

12664999 - 8 = 12664991

So that this does not happen, you must send the parameter as text ( String ) and interpret it as such.

Pd: I only add \" so that, in the HTML generated by PHP, it will show

"<td> <button type='button' class='btn btn-primary' onclick='CrudNotas("12664999-8")' > <span class='glyphicon glyphicon-plus' aria-hidden='true'> </button> </td>"

You comment on any questions.

    
answered by 06.12.2017 / 02:29
source