I have some HTML code I need to style that is being automatically generated by a web service. Part of the code I’m given looks something like this:
<input type='text' id='txtField001' class='txtField150'>foo</input>
<input type='text' id='txtField002' class='txtField10'>bar</input>
<input type='text' id='txtField001' class='txtField142'>sum</input>
Now, here comes the “weird” part: the numbers that follow the class name (i.e.: 150, 10 and 142) are, in fact, the max. number of characters the text field accepts, slo it gives me a “hint” as to how wide the text field should render: wide for 150, shorter for 10; since this number varies wildly (it’s user defined by whomever is calling the web service) it is not practical to have “n” classes to comply with all possible values.
So: is there a way to have a “ranged class” or something like that – keeping in mind that, theoretically, I cannot change whatever the web service is delivering, and that I don’t really want to evaluate things with javascript?
Concretely, is there any way to declare something like this (I know it’s somewhat wild):
.txtField1 ... .txtField50 {
width: 50px;
}
.txtField50 ... .txtField100 {
width: 80px;
}
.txtField100 ... .txtField150 {
width: 120px;
}
(how I’m reading this in my mind: “for any class txtField ranging from 1 to 50, use a 50px width… and so on)
Thanks for any help. I know it’s a long shot, and that my best option is to use javascript, but hey, I had to ask 😉
Yes, with Limits
I have been pondering a solution that is similar to cimmanon’s, only I knew it needed to be far more refined than that (which is why it has taken some time to answer).
Let me state up front that this probably needs a practical limit (I don’t know if there is a limit on the number of characters that it can be in your situation). As you can see in my example fiddle, anything 300+ fails to resolve to larger sizes. If there is a high or unknown upper limit, then javascript is really going to be your best solution. My example works for less than 300, and perhaps up to 999 could be made with not too much more code. But 1000+ I think would be unreasonable.
CSS