2

I have an input text that I need detect when the text (value) change. The value can be changed in two ways:

  1. Allow to the user write in it.
  2. The user click a button, runs a jQuery event and fill it with the result.

For the item 1 I solved using keyup but for the item 2 I have no idea how can I detect when the text is changed. I try change() but does not work.

4
  • 1
    Why not just call the extra code after you populate the input? You can use $("#id").trigger("keyup"); Commented May 29, 2013 at 14:03
  • 1
    He's probably using some library/widget... Commented May 29, 2013 at 14:05
  • Can you be more specific, what kind of (jQuery) event does script respond to and whether the result is created and populated synchronously? Commented May 29, 2013 at 14:05
  • Nice, four answers, each suggesting a completely different solution :). Wondering which one will work :) Commented May 29, 2013 at 14:19

5 Answers 5

1

You can use .trigger() inside your function where you fill the input.

$("#idOfButton").click(function() {
    //do some stuff
    //fill input
    $("#edit").val("new stuff!").triggerHandler("keyup"); //<--Trigger keyup event
});

Demo: http://jsfiddle.net/Q8fCa/1

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

1 Comment

would triggerHandler not work better, as you dont want the element to know about the function call? api.jquery.com/triggerHandler
0

Add event handler on change() for item2 and in item1 keyup handler, trigger item2 change() event. Simple like that...

Comments

0

"runs a jQuery event and fill it with the result."

you can call another function from your jquery event.

$('sel').click(function(){
textedited();  // call your new function. 
});

Comments

0

You can create an interval that checks whether the value of the input field has changed in every occasion (user input or button click) :

var oldValue = $('#field').val();
var newValue = oldValue;

var checkInputFieldInterval = window.setInterval(function() {
    newValue = $('#field').val();
    if (newValue !== oldValue) {
        // VALUE CHANGED, DO STUFF
        oldValue = newValue;
    }
}, 100);

Note the the "100" is the interval period in ms. You can find more information about the setInterval() method on this page http://www.w3schools.com/jsref/met_win_setinterval.asp

Comments

0

You can do...

$('.name').on('keyup', function(){
  /* I'll run when the button is clicked or there's a keyup event. */
})

$('button').on('click', function(){
  $('.name').val('Bill').trigger('keyup');
})

Here's a quick demo: http://jsbin.com/uteceb/1/edit

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.