Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda