Link to home
Start Free TrialLog in
Avatar of Prem Prakash
Prem Prakash

asked on

issue with parent checkbox value with node.js cant be read using java script

Node.js and EJS

I have a html page in node js using ejs template wherein i have main checkbox for Hobbies(x) with id hobby with checkbox check by default once the page loads .and underneath i have 5 options(a,b,c,d,e) . these sub checkbox underneath hobbies are also checked by default.

I am wondering how do i write a java script function inside node.js router so that when box is unchecked no result should get displayed in node.js console and i should be able to send data to mongodb collection.

Please note that i am able to access the checkbox with id req.body.hobbyid when selected and send this data to backend server. However when i unselect the parent checkbox i am getting that value is undefined in node.js console.

Code snippet below

if (req.body.hobby[0]) {
      console.log(a); //here x can be any value
}
if (req.body.hobby[1]) {
      console.log(b); //here y can be any value
}
if (req.body.hobby[2]) {
      console.log(c); //here x can be any value
}
if (req.body.hobby[3]) {
      console.log(d); //here y can be any value
}

// code to connect to mongodb which i am not giving here which works very well when i check the main checkbox for hobby.

I want to achieve a way wherein when main parent checkbox(here which is hobby with id hobbymain is unchecked it should store a value as "False" and set the value as "Not submitted" in db and store in some variable which i can later pick the send to backend server.

Please note that when all checkbox are checked it works perfectly fine . However if i uncheck main Hobby checkbox all children checkbox (a,b,c,d) are unchecked and it throws me exception saying invalid reference.


A Prompt reply would be helpful
Avatar of Prem Prakash
Prem Prakash

ASKER

Got my own
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.