1

In the following code i want to know how to detect when text is inserted dynamically in input field

<input id="test"><br /> 
<a href="#">Click me</a>

$("#test").on("input", function() {
  alert("Change to " + this.value);
});
$("a").on("click", function() {
  $("#test").val("In clicks we trust.");
});

line to the code

3
  • Dynamically inserted? As in programatically from the code?? Commented Aug 27, 2013 at 17:35
  • "The title and the code should say it all.", Sorry, but I don't get it.. :( Commented Aug 27, 2013 at 17:35
  • @tymeJV I don't know what programatically means, but perhaps I have misused the term dynamically. What I wanted to say is that the value inserted into the input field comes from the code and is not typed. Commented Aug 27, 2013 at 17:40

2 Answers 2

2

Listen for a change event, also, you'll have to trigger this after programatically adding text:

$("#test").on("change", function() {
    alert("Change to " + this.value);
});

$("a").on("click", function() {
    $("#test").val("In clicks we trust.").change();
});

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

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

3 Comments

@naomik -- There's always one :\
That fires whether the input was changed directly by the user, or through code. If he really needs to tell if an input was changed via code, then I think the only way to do that would be by registering a custom event
change is not fired when text is inserted from Windows's Tablet OSK in handwriting mode.
1

jQuery change() event handler

$('#test').on('change', function() {
    ...
});

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.