27

Let's say you have a simple Text Box and button on your page. Clicking on the button will update the textbox value:

HTML:

<input type="text" id="myText" val="" />
<input type="button" id="myBtn" value="Change TextBox Value" />

jQuery:

$("#myBtn").on('click', function() {
   $("#myText").val('adsf');
})

I'd like to add an event listener for the change event of the textbox, like so:

$('#myText').on('change', function(){
    alert('Changed!')
});

That alert does not get triggered when you click the button but it does get triggered when you manually change the textbox value and focus out of it.

I know you can add $("#myText").trigger('change') to the onclick event of the button, but I wish to only accomplish this by adding an event listener to the textbox.

jsfiddle

Thanks

5
  • Just as clarification - you want the alert triggered by both manually changing the text box and by clicking the button? Commented Apr 16, 2013 at 22:19
  • He wants the alert be triggered not by the click itself, but by the change within the textbox. Commented Apr 16, 2013 at 22:20
  • Hanlet is correct. any change from anywhere Commented Apr 16, 2013 at 22:22
  • @Sammy, just out of curiosity, what exactly are you trying to accomplish with this? This has been asked previously btw, and no real answer was provided, I am guessing the same is going to happen to this question: stackoverflow.com/questions/1481152/… Commented Apr 16, 2013 at 22:30
  • 1
    @HanletEscaño, I have a bunch of text boxes on a page that get updated based on various actions (not necessarily by user input). I need to know whenever these textboxes get updated. Since there are several ways that the text boxes can get updated I was trying to avoid adding a manual triggering for each one, but it seems I don't have another choice. Commented Apr 16, 2013 at 22:39

5 Answers 5

19

There is no such event supported. The only thing you can try is to set interval or timeout to check if the value was changed (using data to store temporary value):

var $myText = $("#myText");

$myText.data("value", $myText.val());

setInterval(function() {
    var data = $myText.data("value"),
        val = $myText.val();

    if (data !== val) {
        $myText.data("value", val);
        alert("changed");
    }
}, 100);

DEMO: http://jsfiddle.net/xZcAr/1/

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

4 Comments

Great idea, however, I am not sure what kind of performance hit the page would take if you had a lot of text boxes on the page.
@VisionN - Thanks for the help. I will accept this as the answer, since it will technically work but not the preferred method unless I go with manual triggering as you suggested.
This by far looks the simplest and cleanest solution. The only issue seems to be perf. Will a setIntervalMethod eventually clog up the webpage and make it slow?
@SexyBeast No, it won't. Modern CPUs can easily work with multiple background tasks without affecting the general performance. Moreover, interval of 100 milliseconds is long enough, it won't slow the webpage.
4

Why not force a manual event when the button is clicked like so -

$('#myText').on('change',function(e){
    alert('Changed!');
});

$("#myBtn").on('click', function() {
    $("#myText").val('adsf');
    $("#myText").change();
})

1 Comment

triggering change is a pretty good option, whenever regular events not firing.
0

You'd need to connect both handlers. To reuse the handler you can extract it to a named function:

function showChanged(){
    alert('Changed!')
}

$("#myBtn").on('click', showChanged);
$("#myText").on('change', showChanged);

2 Comments

Thanks Kenneth. This will work, but I only wish to accomplish this by adding an event listener to the textbox not the button
I'm afraid that apart from raising the event yourself, this is not possible
0

You need to simply use keyup function:

$( "#myText").keyup(function(){
   alert($(this).val());
});

Comments

0

You can do something like this. Instead of listening for an input field listen to a change on div that gets updated att same time.

function trigger(){
setTimeout(function(){ 
document.getElementsByName("Thing")[0].value = 'hello'
document.getElementById('myDiv').innerHTML = document.getElementsByName("Thing")[0].value 

}, 3000);
}

trigger()
document.getElementById("myDiv").addEventListener('DOMSubtreeModified', (e)=>{
  console.log(e.target.innerHTML)
 
});
<input type="text" name="Thing" value="" />
<div style="display:none;" id="myDiv"></div>
  

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.