Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to style part of an input field's value? [duplicate]

I'm working with an <input> field and I'd like to style part of the field as the user's typing in a different color. For example, let's say the <input> has a style declaration of color: red; and I want to change part of it to color: blue;. Is there any way this is possible?

If there isn't (as I suspect), any creative ideas on how I can simulate this effect while still preserving semantic mark-up?

like image 440
Josh Leitzel Avatar asked Jun 25 '10 21:06

Josh Leitzel


People also ask

How do you style a specific input?

If you only want to style a specific input type, you can use attribute selectors: input[type=text] - will only select text fields. input[type=password] - will only select password fields. input[type=number] - will only select number fields.

How do I copy values from one input field to another?

Simply register an input even handler with the source textfield and copy the value to the target textfield .

How do you change the input field?

Sometimes we need to set a default value of the <input> element, This example explains methods to do so. This property set/return the value of value attribute of a text field. The value property contains the default value, the value a user types or a value set by a script.

How do you make input value unchangeable?

You can use readonly attribute, if you want your input only to be read. And you can use disabled attribute, if you want input to be shown, but totally disabled (even processing languages like PHP wont be able to read those). When you send form to a php file, it won't read disabled inputs.


2 Answers

Your suspicions are correct: styles will apply to the whole input only.

As styles can apply to the entirety of an element only, a solution will require at least one element per required colour.

Consider the division of the input field with respect to the point at which the user is making changes. There are three sections of the input:

  • that before the point at which changes are being applied
  • that after the point at which changes are being applied
  • that at the point the changes are being applied

You cannot achieve this with a single input element. And as the point at which the changes are being applied can change, the portions of the 'input' wrapped by the three elements will also change. JavaScript is required for a solution.

You should initially include a regular input element and forgo any of the required colouring. Use JavaScript to replace the input with a suitable container element. This can be styled to mimic an input element.

As changes occur, use JavaScript to identify the above-mentioned three divisions. Wrap them in suitable elements (spans would be ideal) and colour as needed.

Consider the following starting point for the generated replacement markup:

<div class="input">   <span class="nonEdited before">foo</span>   <span class="edited">fizz</span>   <span class="nonEdited after">bar</span> </div> 

Use click, keydown and keyup events to figure out the three divisions for the input and to apply wrap the three portions of the faked input as required.

like image 96
Jon Cram Avatar answered Sep 21 '22 19:09

Jon Cram


As others have said, you can't do this with styles and static markup.

You could probably do it with a Flash-based form.

But, if I had to this, I'd use jQuery to overlay divs, with the colorized text, atop the <input>.

Algorithm:

  1. Use a normal <input> with whatever default styles are desired. The contents of this input will never change except by user action.

  2. jQuery monitors that <input>. When it detects trigger word(s), it adds a <div> after the input and fills it with the trigger word(s) -- styled as desired. Probably one <div> per word or phrase is best.

  3. jQuery then positions the new <div>, absolutely, directly over the trigger word(s).
    Getting the trigger word(s) offset within the <input> might not even be necessary, because the previous words could also be in the overlay <div> -- either styled defaultly or with visibility: hidden.
    But, if only the trigger word(s) are desired in the overlay, then using a fixed-width font, like Courier, will help with the sub-positioning.

  4. Take care that the overlay does not interfere with the user trying to mouse or key to certain parts of the <input>. IE, probably don't want to cover any more of the <input> than necessary, and set a click() handler to relay focus.


Alternate, user friendly and simpler approach:

Rather than try to do funky, non-user-expected things to the input, take a page from Jakob Nielsen and from sites like StackOverflow.

Just have a plain ol' <input>, but underneath it, show the formatted text as it comes in.

like image 40
Brock Adams Avatar answered Sep 21 '22 19:09

Brock Adams