0

How to disable enable button with change of input value. I want to enable button with Change in #second read-only input field, by changing its value remotely or from other input field.

$(document).ready(function(){
$('#button').attr('disabled',true);
});
$(document).on('change keyup blur','#first',function(){
   var second= $('#second').val();
   if($(this).val() != '' ){
		sum = parseFloat($(this).val()) + parseFloat(second);
    $('#second').val(sum);
    }
});
$("#second").on('change',function(){
   var second= $(this).val();
   var third= $('#third').val();
  if (second == third) {
     $('#button').attr('disabled',false);
  } else {
	$('#button').attr('disabled',true);	
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" id="first" placeholder="first">
<input type="number" id="second" value="5" readonly>
<input type="number" id="third" value="10" readonly>

<input type="button" id="button" value="button" />

2 Answers 2

1

Try this
https://www.w3schools.com/code/tryit.asp?filename=G5YTJB50T072

In your code you used keyup, change, blur which makes calculation incorrect and alway use jquery code like change function inside document.ready.

Thank You

    <script>
        $(document).ready(function(){
            $('#button').attr('disabled',true);
            $("#second").on('change',function(){
                var second = $(this).val();
                var third = $('#third').val();
                if (second == third) {
                    $('#button').attr('disabled',false);
                } else {
                    $('#button').attr('disabled',true); 
                }
            });
        });
        $(document).on('keyup','#first',function(){
            var second= $('#second').val();
            if($(this).val() != ''){
                sum = parseFloat($(this).val()) + parseFloat(second);
                $('#second').val(sum);
                $('#second').val(sum).trigger('change');
            }
        });
    </script>
Sign up to request clarification or add additional context in comments.

Comments

0

Your issue is in this line:

$('#second').val(sum);

Changing an input value does not trigger a change event. So, you can trigger it by yourself:

$('#second').val(sum).trigger('change');

$('#button').attr('disabled', true);
$(document).on('change keyup blur', '#first', function () {
    var second = $('#second').val();
    if ($(this).val() != '') {
        sum = parseFloat($(this).val()) + parseFloat(second);
        $('#second').val(sum).trigger('change');
    }
});
$("#second").on('change', function () {
    var first = $("#first").val();
    var second = $(this).val();
    var third = $('#third').val();
    if (second == third) {
        $('#button').attr('disabled', false);
    } else {
        $('#button').attr('disabled', true);
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<input type="number" id="first" placeholder="first">
<input type="number" id="second" value="5" readonly>
<input type="number" id="third" value="10" readonly>

<input type="button" id="button" value="button">

2 Comments

can it work with document.onload, because it can be change by another input also.
Yes, you need to wrap into document.onload

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.