3

Here's the code:

$("input[type='text']").on('input', function() { ...stuff here... });  //first function

$('input[type="text"]').click(function() {  //second function
  $('#keyboard').show();
  $('#keyboard .letter').click(function() {
    var currentValue = $('input[type="text"]').val();
    alert(currentValue);
    $('input[type="text"]').val( currentValue + $(this).text() );
  });
});

The short version is, when I type in this text input, the top function fires. In the second function, I make a keyboard appear, and change the value of the input based on user clicks. However, the first function is not firing when the input value is changed by means of the second function. Any ideas? Much appreciated.

0

1 Answer 1

14
$("input[type='text']").trigger('input');

Put that in your second function. Merely setting the value of a textbox using javascript will not trigger the input event, thus your input event handler will not fire. In this scenario, you need to trigger that event handler manually.

Note: You should not be using the input event handler. See this link and look at the the awful browser compatibility. https://developer.mozilla.org/en-US/docs/DOM/window.oninput . You will probably be better served by the change event instead.

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

5 Comments

Agreed. this should invoke the change event not input.
I didn't even know there was an oninput event!
I am making packaged app for Chrome, so I only care about webkit. .on('input') works there. Although I did change that to .change() and it would not work. No idea why. Thanks for the answer.
Changing the value of an input element using JavaScript, using .val() for example, won't fire the .change() event. Documentation: api.jquery.com/change
It does fire the event in IE 10 and 11. But not edge. (not sure on older versions).

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.