Tell me, please: The input field is given.

<input id="sum" type="text" onChange="add_r('р.')" /> 

In the input field, should always be the letter r. (rubles). When you enter the amount in this field, the letter r. must be placed after the numbers.

Here is the javascript code:

 function add_r(sum) { a = document.getElementById('sum'); a.value = a.value + sum; } 

In the above example, the letter "p" is added upon completion of the input, and it is necessary that it be there permanently. This example is written in pure javascript, but must be implemented on jQuery.

Tell me, please, how to implement it on jQuery. My knowledge is not enough, but I am actively learning javascript and jQuery.

I hope for your help.

Sincerely, Alexander

    2 answers 2

    Maybe this option is to make an input field with invisible borders, and immediately after it write "rub." - already outside the field, to the right. All this is wrapped in a div, decorated under the input field, styles, or a picture to put.

    Then there are no difficulties: when editing the amount, the rubles are quietly smoked aside:) Example .

    • An interesting decision. Thank. - Astor

    Use the onkeydown handler. In the simplest case for jQuery, it looks like this:

     $('#sum').keydown(function(){ // Передаем значение в переменную sum var sum=$(this).val(); // Делаем обработку // И записывает это значение обратно $(this).val(sum); }); 

    Still need to provide for the restoration of the position of the cursor in the line.

    • And what, the letter "p." stayed? - Smash
    • In this code, nothing is added to the text. The code demonstrates a general principle. What event to use, how to read and how to write to <input> . - KiTE
    • Dear KiTE, please write a working example. I understand that the format of this resource does not provide for the completion of the task, but since my knowledge at the moment is slightly more than 0 ... In general, I hope for your help. - Astor
    • 2
      How to work with strings can be read here . To read and set the cursor position there is a plugin and another google with its search results. No offense, but the finished code will not be. And the question is not in the format of the forum, but rather in the format of common sense. A novice programmer is more useful to kick in the right direction than a ready-made code. And, by performing the work on a turnkey basis, the main part of the local audience earns money. - KiTE
    • Thank. I agree. - Astor