1

I'm trying to customize the color of an <input> element based on it's current value.

So far I've tried solutions based on this question or this post, but can't get it to work even for a simple condition (e.g. "1<2"), and so even less based on the input's value (e.g. "value<2").

Here's a jsFiddle illustrating the problem, and my attempts below:

    <p>
Simple coloring
      <input autocomplete="off" id="spinner-twenty"  style="background-color:red" class="spinner-twenty" name="spinner" value="0.1">
    </p>

    <p>
Conditional coloring based on style condition
      <input autocomplete="off" id="spinner-twenty"  style="${1} < 2 ? 'background-color:red' : 'background-color:white'" class="spinner-twenty" name="spinner" value="0.1">
    </p>

    <p>
   Conditional coloring based on background-color condition

      <input autocomplete="off" id="spinner-twenty"  style="background-color:{!IF(1 < 4,'#7CFC00', '#FFA07A')}" class="spinner-twenty" name="spinner" value="0.1">
    </p>    

  Conditional coloring based on background-color condition (based on inputs "value")

      <input autocomplete="off" id="spinner-twenty"  style="background-color:{!IF(value < 4,'#7CFC00', '#FFA07A')}" class="spinner-twenty" name="spinner" value="0.1">
    </p>    

2 Answers 2

1

You could use jQuery .change function which will be triggered on input value change,

But in your case which you are using spinner when a value changed it add a new attribute to the input called aria-valuenow="" so you can't trigger on value change because there is .value() but no .aria-valuenow(),

So far I created new default input for testing and it worked greate, see the updated JSfiddle.

Update

You could use .on("spinstop",...), see the updated JSfiddle.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the hints! You're correct, the .change wouldn't work on the spinner, but I found in this answer that another event could be used: .on("spinstop",...). See the updated JSfiddle for a final working solution (i.e. conditional formatting on spinner input's value). If you update your answer with the new change event I can mark it as accepted.
Well done! you are most welcome and btw I have updated the answer.
1

The resources you link to are describing server-side logic (two different kinds, neither of which you appear to be using). They determine what HTML should be output before it leaves the server.

Browsers do not support logic in style attributes.

To achieve this you will need to write some JavaScript. You could binding an Event Listener to the element that fires when there is an input event, reads the value property of the element and then sets the style.backgroundColor property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.