1
    $(document).ready(function() {
        $('#text').live('change', function() {
            alert('hello');
        });
        $('#button').live('click', function() {
            $('#text').val('some text');
        });
    });  

    <div>
        <input type="button" id="button" value="Click Me" />
        <input type="text" id="text" />
    </div>

How can I make the change function on the textbox trigger when I click the button?

1 Answer 1

5

You can use .change() (the shortcut) or .trigger(), like this:

$('#button').live('click', function() {
  $('#text').val('some text').change();
});

Or this:

$('#button').live('click', function() {
  $('#text').val('some text').trigger('change');
});

Either method works to trigger any change event handlers you've added, you can test it here.

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

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.