1

if select option select fixed value then input field type rupees value support only 20 digit with rupees symbol. if select percentage then replace rupees symbol with percentage of symbol and value type only 2 digit input value with percentage symbol without click a button

my this code working properly but i am facing two small problem .1 if my webpage load default value set fixed (but input value and rupees symbol not working
.2 if change value then working both condition but value not reset automatically changing state. and if user select percentage single then not change percent value again please check anyone of my code here is the similar my code

 <!DOCTYPE html>
<html>
<head>
    <title>Input Field</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#dropdown').change(function() {
                var selectedOption = $(this).val();
                if (selectedOption === 'fixed') {
                    $('#inputField').attr('maxlength', '20');
                    $('#inputField').on('input', function() {
                        var value = $(this).val();
                        var formattedValue = '₹' + value.replace(/\D/g, '').replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
                        $(this).val(formattedValue);
                    });
                } else if (selectedOption === 'percentage') {
                    $('#inputField').attr('maxlength', '2');
                    $('#inputField').on('input', function() {
                        var value = $(this).val();
                        var formattedValue = value.replace(/\D/g, '') + '%';
                        $(this).val(formattedValue);
                    });
                }
            });
        });
    </script>
</head>
<body>
    <select id="dropdown">
        <option value="fixed">Fixed Value</option>
        <option value="percentage">Percentage</option>
    </select>
    <input type="text" id="inputField">
</body>
</html>
2
  • but if i am select percentage value and i am clicking backspace button then value not cut of automatically Commented Aug 12, 2023 at 8:43
  • and also not remove percentage if i am pressing backspace then cut value and after putting value automatically insert percent symbol in value last Commented Aug 12, 2023 at 8:59

1 Answer 1

0

Lets make things easier and create separate function and based upon that function apply your logic on text field.

Example:

var selectedOption = '';
$('#dropdown').change(function() {
  selectedOption = $(this).val();
  check(selectedOption);
});
$('#inputField').on('input', function() {
  selectedOption = $('#dropdown :selected').val();
  check(selectedOption);
});

function check(inp) {
  var _inp = $('#inputField');
  var value = _inp.val();
  if (inp === 'fixed') {
    _inp.attr('maxlength', '20');
    var formattedValue = '₹' + value.replace(/\D/g, '').replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
    _inp.val(formattedValue);
  } else {
    _inp.attr('maxlength', '2');
    var formattedValue = value.replace(/\D/g, '') + '%';
    _inp.val(formattedValue);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="dropdown">
  <option value="fixed">Fixed Value</option>
  <option value="percentage">Percentage</option>
</select>
<input type="text" id="inputField">

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.