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

238
Vistas
Js function argument depend upon the submitted value in EJS

I am building an express app. I have a drop-down whose submitted value is passed to users.js and initiates a mongodb query. Then, result of that query is passed back to ejs and becomes an argument of a script function.

The problem is I want that function to be called when I click on Submit. (I can afford the delay in rendering of page).

Right now, I have two buttons, one for submit and one for calling that function.

index2.ejs


    <form action="/users/details" method="POST">
      <fieldset>
      <select required class="selectpicker" name="selectpicker">
            <!-- options -->
        </select>
        
      <input type="submit" id="submit-form" />
      </fieldset>
  </form>


  <button onclick="popu(<%= JSON.stringify(coun) %>);">blue</button>

and users.js

router.get("/index2", function(req, res, next) {

  (async function() {
      count = await funcoun(coll); // a mongodb query
      res.render("index2", {title: 'some data', userdataa: a, imgsrc : coll, coun : count});
    }) ();
});

router.post("/details", function(req, res, next) {
    coll = req.body.selectpicker;
    res.redirect("/users/index2");
})

So, basically I want the form to pass value (of coll) to express, calculation of count, calling of function with this value, and then rendering the page.

Currently, calculation of count is done on basis of old value, and when blue button is clicked then present value.

I am confused that if I put onclick in submit button, what will it do since coun is not yet calculated.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

<body onload = "popu(<%= JSON.stringify(coun) %>);">

solved the issue

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