1

what is the easiest way to communicate with a button click to node js

I just want to redirect to a page when the button is pressed. How Can I do that?

Here is my Html code:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="app.js"></script>
</head>
<body>
    <button onclick="pay()">Pay</button>
</body>
</html>
  • Its depends on the use of feature/action, if its internal and something transaction related or secure link then you can use internal redirection functions features, if its external link then you can add direct link in href in anchor tag. – turivishal Jun 05 '20 at 15:39
  • Any update on this? – Venkatesh A Mar 02 '22 at 18:20

2 Answers2

0

In case you want to redirect it is not necessary to go through node..... you could use a simple href. But if you still want to do it then follow the steps below:

  1. Create a node express server
  2. Express them in a route
  3. Create an onClick function in jquery in the client
  4. Create an ajax request in that onclick function to the route in the node server.

an example jquery code would be...

`

$("button").click(function(){
$.ajax({url: "demo_test.txt", success: function(result){
    $("#div1").html(result);
  }});
});

`

where 'url: ' would be your route from nodejs

Venkatesh A
  • 1,875
  • 1
  • 19
  • 23
-1

If you want to redirect to a different page I would suggest doing that on the front-end i.e. using tag or window.location.

Instead of a button, you can do something like that:

<a href='linkToYourPage.com'>Link</a>
jhrwekuh
  • 186
  • 2
  • 15