Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having a permanent value in an input field while still being able to add text to it

I don't know if this is possible but I would like to have an input field where I would have a value that is not editable by the user. However, I don't want the input field to be "readonly" because I still want the user to be able to add text after the value.

If you have any idea on how to do this, let me know please that would help me a lot.

EDIT: I use html forms.

like image 727
jaydee Avatar asked Sep 17 '11 07:09

jaydee


2 Answers

You can position the text on top of the input field to make it look as if it is inside it. Something like this:

<input type="text" name="year" style="width:3.5em;padding-left:1.5em;font:inherit"><span style="margin-left:-3em;margin-right:10em;">19</span>

This way your input field will start with "19" which can not be edited, and the user can add information behind this.

Basically what you do is set the input field to a fixed width, so that you know how much negative margin-left to give the span with your text in it in order for it to be positioned exactly at the start of the input field.

You might need to fiddle with the margin-left of the span depending on the rest of your css.

Then also adding pedding-left to the input field, to make sure the user starts typing after your text and not under it.

font:inherit should make sure both your text and the text typed by the user are in the same font.

And if you want to put anything to the right of this input field, do add margin-right to the span with your text, as otherwise other content might start running over your input field as well.

like image 186
Bart Mortelmans Avatar answered Sep 23 '22 08:09

Bart Mortelmans


seems a little weird to me ..why not just use a text output and afterwards the input field?

like sometimes used for the birthdate (although, maybe not anymore..)

birthyear: 19[input field]

edit:

with some javascript stuff you could realise something like that you asked for, though an input field with text and catching keystrokes within that field while only allowing some after what you want to be always there - but, well, you would need to use js ..and if its just for that, Id rather say its not necessary

edit:

if you want to use a trick just for the viewer you could use a background-image/border-style that surrounds a text and the input field, thus making it look like text and input are the same input-box.

like image 32
definitely undefinable Avatar answered Sep 23 '22 08:09

definitely undefinable