1

I have an input field which is being set to readonly by html attribute readonly I am inserting text into that field using jQuery .html() method:

<input type="text" id="myField" readonly="readonly" />

now I have a function myFunction() and I want it to be called when ever the text is being inserted to that field by jQuery.

4
  • you should use the .val() function to assign values to input field. use .onchange function to fire the event when the value is changed Commented Jul 19, 2017 at 7:50
  • if($('#myField').val() !==''){myFunction(); } Commented Jul 19, 2017 at 7:50
  • @RAUSHANKUMAR i have tried onchange but it is not working on input Commented Jul 19, 2017 at 7:53
  • @AlivetoDie where to use this code? Commented Jul 19, 2017 at 7:54

2 Answers 2

2

To achieve what you require you could set the value using val(), then trigger a change event which you can hook an event handler to:

$('button').click(function() {
  $('#myField').val('foo').trigger('change');
});

$('#myField').change(function() {
  console.log('value changed...');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="myField" readonly="readonly" />

<button>Set value</button>

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

1 Comment

Thanks @roryMcCrossan It helped :)
0

I hope this will help you...

<input type="text" id="myField" readonly="readonly" value=""/>

<script>
 $('#myField').on('change', function(){
      if($('#myField').val()!=""){
         myFunction();
      }
 });
</script>

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.