views:

219

answers:

3

I am looking for a way to 'fix' a dollar symbol $ to a text input box eg <input type="text" value="$" /> but make it not possible for the default value to be removed, so that whatever the user inputs, there is always a $ symbol 'prepending' input.

Cheers

+2  A: 

There is the possibility of a background-image but it's difficult to maintain, and doesn't react to font size changes, which makes it the less optimal choice IMO.

A better way in my opionion would be:

  • Put a <span>$</span> next to the input (before it, actually).

  • give it position: relative; left: 20px.

  • The $ sign then moves into the input field.

  • Then, give the input field padding-left: 24px.

  • Voilá! The $ sign is in the input field, does not obscure anything, and cannot be removed.

Pekka
A: 

Is this as a visual effect only? If yes you could apply that with CSS as a background image on the input.

Afterwards, on the server, you can do whatever you want with the value (e.g prepend it with $).

cherouvim
This is a visual thing, to let people know that input will be in $ format e.g $22.00
Eddie
+1  A: 

You can do this with http://digitalbush.com/projects/masked-input-plugin/ using for example:

$('#input').mask('$999.99');
cherouvim