Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8225361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:23:27+00:00 2026-06-07T15:23:27+00:00

How can i get a easy and clear way that set the first radio

  • 0

How can i get a easy and clear way that set the first radio button is checked in Handlebars template. tks

template:

<form>
    {{#each this}}
        <input value="{{value}}" />
     {{/each}}
</form>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

expect render:

<form>
    <input value="val 1" checked />
    <input value="val 2" />
    <input value="val 3" />
</form>

thanks all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:23:30+00:00Added an answer on June 7, 2026 at 3:23 pm

    {{#each}} in Handlebars doesn’t give you access to the iteration number or anything like that so you can’t do it without altering your template and data a little bit:

    <form>
        {{#each this}}
            <input type="radio" value="{{value}}" {{#if sel}}checked="checked"{{/if}} />
        {{/each}}
    </form>
    

    and then add sel values to your data:

    var tmpl = Handlebars.compile($('#t').html());
    var html = tmpl([
        { value: 'val 1', sel: true  },
        { value: 'val 2', sel: false },
        { value: 'val 3', sel: false }
    ]);
    

    Demo: http://jsfiddle.net/ambiguous/27Ywu/

    You could of course just set sel: true on the first element of your data array:

    data = [ ... ];
    data[0].sel = true;
    var html = tmpl(data);
    

    Demo: http://jsfiddle.net/ambiguous/yA5WL/

    Alternatively, use jQuery to check the first one after you have the HTML:

    // Add the HTML to the DOM...
    $('form input:first').prop('checked', true); // Or whatever selector matches your HTML
    

    Demo: http://jsfiddle.net/ambiguous/sPV9D/


    Newer versions of Handlebars do give you access to the index:

    When looping through items in each, you can optionally reference the current loop index via {{@index}}

    {{#each array}}
      {{@index}}: {{this}}
    {{/each}}
    

    For object iteration, use {{@key}} instead:

    {{#each object}}
      {{@key}}: {{this}}
    {{/each}}
    

    So, if you’re using the latest Handlebars, you can do something special by using the fact that:

    1. The first @index will be zero.
    2. Zero is falsey in a boolean context.

    That lets you do this:

    {{#each this}}
        <input type="radio" value="{{value}}" {{#unless @index}}checked="checked"{{/unless}} />
    {{/each}}
    

    Demo: http://jsfiddle.net/ambiguous/PHKps/1/

    Of course, picking out any other index is harder and leaves you either modifying the input data (as before) or adding some sort of {{#if_eq}} custom helper.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working all day and I somehow can't get this probably easy task
I'm sure it's pretty easy, but I just can get it to work. I
This may be an extremely easy question but I can't get my head around
In theory this seems easy but I just can't get my head around this...
This is probably an easy solution but I can't get my head around it.
I know this is an easy concept, but I just can't seem to get
Is there any chance to get a easy way to sync data, in this
Hopefully I can articulate my question well enough to get some clear and usable
Can get all triples with value null in specific field? All people with date_of_birth
We can get class Class object by 3 methods: MyClass.class obj.getClass Class.forName(className) I don't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.