0

How to automatically Add value +1 after keyup?

<input id="tahun1" name="tahun1" type="number"> 
<input id="tahun2" name="tahun2" type="number" readonly>

<script>
$("#tahun1").keyup(function(){
$("#tahun2").val(this.value);});
</script>

for example if I insert "2015" in "tahun1" then in "tahun2" should be "2016" automatically

thanks in advance :)

2 Answers 2

1

Parse the value using Number() or parseInt() then increment the value and update the input field.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="tahun1" name="tahun1" type="number">
<input id="tahun2" name="tahun2" type="number" readonly>

<script>
  $("#tahun1").keyup(function() {
    $("#tahun2").val(Number(this.value) + 1);
  });
</script>

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

1 Comment

@afhamz : glad to help :)
0

Try this

$("#tahun1").keyup(function(){
  
    $("#tahun2").val(parseInt($(this).val())+1);
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="tahun1" name="tahun1" type="number"> 
<input id="tahun2" name="tahun2" type="number" readonly>

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.