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

237
Views
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 answers
Answer question

0

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

solved the issue

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!