Can someone help me out with this please...
I am doing a web form. I would like to insert the dollar sign into some INPUTs that have a numberfield class. I want the user's content to follow the dollar sign or course.
This inserts the dollar sign OK, but the content ends up in front of it.
$('input.numberfield').val('$');
NOTE - the dollar sign is required because this is financial data (nothing to do with jquery! :) )
in other words - someone types in '100' and it becomes '$100')
The plugin “Special Input” creates a responsive keyboard based on jQuery and CSS to insert special characters in textbox (input / textarea). It appends a virtual keyboard with text input, where users can select characters to insert.
The . prepend() method inserts the specified content as the first child of each element in the jQuery collection (To insert it as the last child, use . append() ).
version added: 1.0jQuery( ":input" ) The :input selector basically selects all form controls.
You can do something like this:
$('input.numberfield').each(function() {
$(this).val('$' + $(this).val());
}
In 1.4, a simple more complete solution for your scenario:
$('input.numberfield').keyup(function() {
$(this).val(function(i,v) {
return '$' + v.replace('$',''); //remove exisiting, add back.
});
});
Why make your life complex? Just put the dollar sign into the HTML outside of the input element...
<span style="border: inset 1px black; padding: 1px;">
$<input style="border: none; padding: 0;">
</span>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With