0

I have input with id = "input-price" and element span with some numerical value, e.g. 100. I would like that with each entered character in input, the value of 100 increased by 10 while when I remove the characters, the value decreases by 10. How to do it?

8
  • what do you mean by sign ? Commented Jan 19, 2019 at 12:51
  • I don't understand ? Commented Jan 19, 2019 at 12:51
  • I mean when i remove text from input, value of span element should decrease. Commented Jan 19, 2019 at 12:52
  • "when I remove the sign, the value decreases by 10" which sign are you talking about ? Commented Jan 19, 2019 at 12:52
  • so when you remove a character from the input the value decreases by 10 ? Commented Jan 19, 2019 at 12:52

2 Answers 2

3

Try this -

var priceInput = document.getElementById("input-price");

priceInput.addEventListener("input", representValue)

function representValue(){
   var numOfChars = priceInput.value.length;
   
   var magicSpan = document.getElementById("magic-value");
   value = 100 + (numOfChars*10);
   magicSpan.textContent = value;
}
<span id="magic-value">100</span>
<input id="input-price" type="text">

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

Comments

1

You can try the following way:

var length = $('#input-price').val().length;
$('#input-price').on('input', function(){
  var n = Number($('#spanNumber').text());
  if(length < $('#input-price').val().length){
    $('#spanNumber').text(n + 10);
    length++
  }
  else{
    $('#spanNumber').text(n - 10);
    length--
  }  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="input-price"/>
<span id="spanNumber">100</span>

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.