Search code examples
javascriptjquerybackbone.js

Backbone view change event not firing when input manipulated by code


In my Backbone model's View I have:

events: {
    'change textarea' : 'changeContentTextarea',
    ...

When user enters a text manually into the textarea, it triggers and do this:

    this.model.set('content', this.$el.find('textarea').val());

But it doesn't fire when the content is changed in program (using jQuery):

    txtarea.val(finalText);

and so the model's attribute does not update and only the text in the textarea changes.

Is there any event that I can bind to address this problem? Or how can I fire the event after I change the content using jQuery?


Solution

  • From https://api.jquery.com/change/

    Note: Changing the value of an input element using JavaScript, using .val() for example, won't fire the event.

    So you have to trigger the change event yourself

    $("textarea").val(finalText).trigger("change");