3

I'm making a simple CRUD model with input checkbox. I have no problems on server side, everything is fine. I use NodeJS +MongoDB. But I have problem in editing existing user. When I edit an existing user with a checked checkbox( I get JSON object with parameter checked=true ) how should I display it using JS?
This is part of my users.js file in /routes/ folder

var express = require('express');
var router = express.Router();
var User = require('../../models/User');
var rest = require('restler');
router.get('/adduser', function(req, res){
  var user = new User();
  user.contacts.push({phone: '', email: ''});
  rest.get('http://localhost:3000/api/adduser').on('complete', function(data) {
    res.render('users/add', { title: 'Add New Users' , n: user});
  });
});

And this is views/users/fields.jade part of file for better understanding:

.form-group
label.col-sm-2.control-label(for='email') E-mail:
.col-sm-10
input(type="email", placeholder="email", name="email", value = n.contacts[0].email,required)
.form-group
.col-sm-offset-2.col-sm-10
input#enabled(type="checkbox",style='text-align: center; vertical-align: middle;',placeholder="", name="enabled", value = n.enabled)
| Enable user

So my problem is that I don't understand how I should display that checkbox is really checked when loading existing user.
If user is checked attribute n.enabled=true and if not n.enabled=false. So if user is checked on load of that user I need the input filed to be checked.
I've tried it to do the following way, but it wrote me that n wasn't defined...and I don't know how to pass n as the parameter for that function:

$(document).ready(function(){
  if(n.enabled=="true"){$("enabled").toggle(this.checked);}
});
Nishit Maheta
  • 6,021
  • 3
  • 17
  • 32
Donvino
  • 2,407
  • 3
  • 25
  • 34

2 Answers2

2

In fields.jade, change value = n.enabled to checked = (n.enabled ? 'checked' : '')

Rudi
  • 2,987
  • 1
  • 12
  • 18
  • it is still the same, I need to pass n to this function. But actually I can't get how to do this. – Donvino May 25 '15 at 11:34
  • 1
    Ah ok, maybe in `fields.jade` instead of `value = n.enabled` try `checked = (n.enabled ? 'checked' : '')` – Rudi May 25 '15 at 13:03
  • that woked for me, just posted an answer and then read your comment, thanks. – Donvino May 25 '15 at 13:06
1

Use # for id-selectors and use n.enabled directly to hide or show your element like,

 $("#enabled").toggle(n.enabled);
 //-^ prepend # before id selectors

toggle() will show/hide your element, To check uncheck use the prop() like

$(document).ready(function(){
    $("#enabled").prop("checked",n.enabled);
});
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • Ok, but I still have a problem with passing n to this function. When I run my code it says that n is not defined – Donvino May 25 '15 at 11:32
  • Obviously, you will get this error, if you will not initialize `n` as an object having `enabled` as a `property` in it. When you are using that property, at that time it must be defined. – Rohan Kumar May 25 '15 at 11:36
  • But it's already defined in users.js, isn't it? Because here "input(type="email", placeholder="email", name="email", value = n.contacts[0].email,required)" it works correctly or I just don't understand smth? – Donvino May 25 '15 at 11:52
  • If it is, then make it global so that you can access it everywhere in all other js files. You can try http://stackoverflow.com/questions/3352020/what-is-the-best-way-to-set-a-global-variable-in-javascript – Rohan Kumar May 25 '15 at 12:12