Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call JavaScript function(with parameters) by onclick event

I cant use PHP variables to use as parameters for my function.

The function is:

<script type="text/javascript">
function myFunction(id,name) {
    alert(id);
    alert(name);
}
</script>

And this is PHP part:

echo "<button onclick='myFunction($intvar,$strvar)' type='button'>ClickMe</button>";

the call works when I use numbers like this: myFunction(0,1) but I need to use variables to call myFunction.

Please Help, thanks.

like image 422
Roberto Sepúlveda Bravo Avatar asked Jun 25 '15 06:06

Roberto Sepúlveda Bravo


People also ask

Can we pass parameter in onClick function?

Your onclick event handler can optionally accept a MouseEventArgs parameter which Blazor will automatically include when it invokes the handler.

How do you pass parameters on onClick react?

In order to pass a value as a parameter through the onClick handler we pass in an arrow function which returns a call to the sayHello function. In our example, that argument is a string: 'James': ... return ( <button onClick={() => sayHello('James')}>Greet</button> ); ...

Can JavaScript function accept parameters?

A JavaScript function can have any number of parameters. The 3 functions above were called with the same number of arguments as the number of parameters. But you can call a function with fewer arguments than the number of parameters. JavaScript does not generate any errors in such a case.

How do you pass two values on onClick?

example: <button onclick="updateById(`id`, `name`)">update</button> function updateById(id, name) { alert(id + name ); ... } Save this answer.


1 Answers

You try this function myFunction(ide,name) { change insted of id => ide echo "<button onclick='myFunction(".'"'.$intvar.'","'.$strvar.'"'.")' type='button'>ClickMe</button>";

like image 102
Ambal Mani Avatar answered Sep 27 '22 23:09

Ambal Mani