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

  • SEARCH
  • Home
  • 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 8446005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:48:41+00:00 2026-06-10T09:48:41+00:00

Is it possible to dynamically set the text color of a input field based

  • 0

Is it possible to dynamically set the text color of a input field based on a handlebars.js template value?

I am initially creating my html using this template:

<div class="board">
    <div class="header">
        <span class="name">Project</span>
        <span class="status">Status</span>
    </div>
    {{#each projects}}
    {{> project}}
    {{/each}}
</div>

Where projects is an object read from a db. The resulting html (what gets rendered on the page not what is in my html) for each project looks something like this:

<div class="project">
    <span class="name">FOO</span>
    <span class="status">OK</span>
</div>
<div class="project">
    <span class="name">BAR</span>
    <span class="status">NOTOK</span>
</div>

I would like to render this html with the colour of the OK & NOTOK text set dynamically.

I already have a handlebars helper function that will successfully return the correct colour code based on each option and I can call this using:

{{getStatusColor currentStatus}}

What I would like to do is put this function call directly into the css itself, a bit like:

font-color: {{getStatusColor currentStatus}}

But obviously this doesn’t work. This function does feel like the right approach though but where can I use it to format the text correctly on the page?

  • 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-10T09:48:43+00:00Added an answer on June 10, 2026 at 9:48 am

    Answering my own question: The template needed to be expanded (from {{> projects}}) to allow for conditional rendering.

    <div class="board">
        <div class="header">
            <span class="name">Project</span>
            <span class="status">Status</span>
        </div>
        {{#each projects}}
        <div class="project">
            <span class="name">{{name}}</span>
            <span class="status" style="color:{{getStatusColor status}}">{{status}}</span>
        </div>
        {{/each}}
    </div>
    

    For completeness, the helper function getStatusColor looks like this:

    Handlebars.registerHelper('getStatusColor', function(status) {
        switch (status) {
            case "GOOD" : {
                return 'green';
            }
            break;
            case "BAD" : {
                return 'red';
            }
            break;
            default : {
            ...etc.;
        }
    });
    

    UPDATE:
    In the interests of honesty, I should confess I totally missed that I already had this expanded template in my code and that {{> projects}} was pointing to this. I should have just added the style="color:{{getStatusColor status}}" attribute directly into the referenced project template. So, as much for my benefit as others, the final, working, HTML:

    <template name="foo">
        <div class="board">
        <div class="header">
            <span class="name">Project</span>
            <span class="status">Status</span>
        </div>
        {{#each projects}}
        {{> project}}
        {{/each}}
        </div>
    </template>
    
    <template name="project">
        <div class="project {{selected}}">
            <span class="name">{{name}}</span>
            <span class="status"style="color:{{getStatusColor status}}">{{status}}</span>
        </div>
    </template>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to dynamically set a Spring MVC portlet:actionURL portlet:param using javascript? I
Is it possible to create a new List<T> where the T is dynamically set
Is it possible to make changes to a CSS rule-set dynamically (i.e. some JS
Is it possible to dynamically set the view base type for a partial view?
Using answer from Set rownumbers to false dynamically in jqgrid I created row numbers
Is it possible to dynamically access properties by using #evaluate? I apologize in advance
Is it possible to dynamically set the 'extra' option in the Django Admin Inline?
I am creating a form using only HTML and JavaScript where I want to
is it possible to dynamically set the datacontract's datamember attributes during runtime? Basically what
Is it possible to dynamically create a select statement with multiple where() clauses? Suppose

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.