GibboK
GibboK

Reputation: 73988

How to avoid firing event 'change' when setting a value properly for a dijit/form/TextBox?

I need to change the property value for a widget dijit/form/TextBox, but I have notice what whenever I use widget.set('value','some value'); event change is fired on widget.

I need to programmatcally change that properly but without triggering change event. Any idea how to achieve it?

https://jsfiddle.net/s5620bwd/6/

require(['dijit/form/TextBox', 'dojo/on'], function(TextBox, on) {
  var textBox = new TextBox({
    intermediateChanges: true
  }, 'button');
  on(textBox, 'change', function(value) {
    this.set('value', value);
    console.log(this.get('value')); // widget updated
  });

  // change value programmatically
  textBox.set('value', 'value change programmatically');
});

Upvotes: 1

Views: 1013

Answers (1)

mmckenzie
mmckenzie

Reputation: 121

The TextBox widget has a property called textbox which corresponds to the input element contained in the widget. If you set the value on the element it will not trigger any event listeners.

require(['dijit/form/TextBox', 'dojo/on'], function(TextBox, on) {
  var textBox = new TextBox({
    intermediateChanges: true
  }, 'button');
  textBox.on('change', function(value) {
    // this event handler will not fire
    this.set('value', value);
    console.log(this.get('value'));
  });

  textBox.textbox.value = 'my new value';
});

Also, to attach an event handler to a widget, you should use the widget's own on method to attach the handler.

Upvotes: 4

Related Questions