0

While updating a record in node js , i am able to render value for text input fields but in the update form the value of radio button and textarea is not coming. Can any one please let me know how to implement it. I am using hbs template.

<div class="form-group row">
    <label for="inputPassword3" class="col-sm-5 col-form-label" style="color: #2874a6 ;">Owner Name</label>
    <div class="col-sm-6">
        <input type="email" class="form-control" name="owner" value={{product.owner}}>
    </div>
</div>
<div class="form-group row">
    <label for="inputPassword3" class="col-sm-5 col-form-label" style="color: #2874a6 ;">Description</label>
    <div class="col-sm-6">
        <textarea class="form-control" name="description" rows="2" value={{product.description}}></textarea>
    </div>
</div>

<div class="form-group row">
    <label for="inputPassword3" class="col-sm-5 col-form-label" style="color: #2874a6 ;">Summary column format</label>
    <div class="col-sm-6">
        <div class="radio-inline">
            <input class="form-check-input" type="radio" name="taskDone" value="Yes" onClick="changeresult();">if({{product.team}}=="Yes"){ %> checked
            <%} %>> Yes </input>

        </div>
        <div class="radio-inline">
            <input class="form-check-input" type="radio" name="taskDone" value="No" onClick="changeresult();">if({{product.taskDone}}=="Yes"){ %> checked
            <%} %>> Yes </input>

        </div>
    </div>
</div>

The value for textarea field and radio button is not coming. Thanks for your help in advance

dusthaines
  • 1,320
  • 1
  • 11
  • 17
Tiya
  • 1
  • 4
  • Are you certain you're using the helper appropriately? [The docs suggest](https://handlebarsjs.com/guide/builtin-helpers.html#if) a method that would look more like this: `{{#if product.team == "Yes"}} checked {{/if}}`. Your example looks more like `EJS` than `handlebars`. I only have a passing familiarity with handlebars but it's a best guess after spending some time with the docs. – dusthaines Apr 23 '20 at 20:45
  • I edited my code as per your suggestion Yes but getting below error Parse error on line 125: ..." {{#if ticket.team == "Yes"}} checked { -----------------------^ Expecting 'CLOSE_RAW_BLOCK', 'CLOSE', 'CLOSE_UNESCAPED', 'OPEN_SEXPR', 'CLOSE_SEXPR', 'ID', 'OPEN_BLOCK_PARAMS', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', 'SEP', got 'EQUALS' – Tiya Apr 24 '20 at 06:24

1 Answers1

0

I don't work with handlebars regularly so you might need to do additional research.

It seems like maybe {{if}} doesn't support comparisons, only existence and booleans. So you have a couple of options based on a bit of research:

Option 1 : Boolean values
You could provide the values to the template as booleans. So rather than "Yes", "No", have the middleware (or even better update your database schema and store them accordingly) provide them as true, false. Then you could use:

{{#if product.team}} checked {{/if}}

Option 2 : Register a helper

Register helper in your javascript code:

Handlebars.registerHelper('ifEquals', function(arg1, arg2, options) {
    return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
});

Use in template:

{{#ifEquals product.team "Yes"}} checked {{/ifEquals}}

You can read more via this question

dusthaines
  • 1,320
  • 1
  • 11
  • 17