How to send a php variable in a javascript function parameter? [duplicate]

0

I want to send a php variable when calling a javascript function. This way:

href="javascript:calcular($valor1)"

I try to do this, it does not seem to work. Is there any other way to do this?

    
asked by anonymous 16.09.2016 / 19:36

2 answers

1

Just print the variable using the echo

href="javascript:calcular(<?php echo $valor1; ?>)"
    
16.09.2016 / 19:40
1

Hello, I would do so:

function calcular(element){
  var variavel = $(element).data('var');
}
<a data-var="<?= $calcular ?>" onClick="calcular(this)">Clique</a>
    
16.09.2016 / 20:29