Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Is it possible to pass my value in node and express js by clicking a button

In this problem, I want to pass my values in express js so that it will written down in node js. I am clicking a button which will be the demo here

<form action="/measure" method="POST">
    <button type="button" name="Add" > Add </button>
    <button type="button" name="Substract" > Subtract </button>
    <input type="submit">
</form>

I did something like this in express js

const homePage = fs.readFileSync('./index.html')

app.get('/',(req,res) => {
    res.writeHead(200,{'content-type':'text/html'})
    res.write(homePage)
    res.end()
})

var counter = 0;

app.post('/measure',(req,res) => {

    res.writeHead(200,{'content-type':'text/html'})
    const {Add,Subtract,result} = req.body
    if (Add) {
        counter += 1;
    } else if (Subtract) {
        counter -= 1
    }
    res.write(homePage + `<h1>${counter}</h1>`)
    res.end()

})

But this doesn't do anything, based on my thought, the wrong about here is the button in index html which it doesn't pass anything...Have you guys any idea how I can pass the value in the express js? I've been searching since last night about it but couldn't find the right information, All I get is this link which is not my prefer since I am not focusing yet in database LINK

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A single button in a form can be represented as a <button> element. But if you want several buttons in a form, represent them all as <input type="submit">:

<form action="/measure" method="POST">
  <input type="submit" name="Add" value="Add"/>
  <input type="submit" name="Subtract" value="Subtract"/>
  <input type="submit"/>
</form>

Depending on which button you press, you will get req.body as

  • {"Add": "Add"} or
  • {"Subtract": "Subtract"} or
  • {}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!