Note that there are some explanatory texts on larger screens.

plurals
  1. POJavasScript textbox value not updated until focus is lost
    text
    copied!<p>I'm using Dojo for this but suspect this issue is native to JavaScript rather then dojo specific. I've created a text box and registered a function to be called when the onInput event is fired (I've tried this with onKeyPress, onKeyUp and onKeyDown, so essentially all the events that would be fired when the user enters text).</p> <p>The event is fired so that is working great. The problem is that in the method I reference the textboxes value and it isn't getting updated. The textbox value is only updated if the textbox loses focus. When I then type in the box again I get the value as it was after losing focus, i.e. not including the additional keypress that fired the event. So, this is the code being called onInput:</p> <p><code>console.log("searching for: " + this.textbox.value);</code></p> <p>when I type "tes" into the (initially empty) text box, I'get three lines in the console log as follows:</p> <blockquote> <p>LOG: searching for:<br> LOG: searching for:<br> LOG: searching for:<br></p> </blockquote> <p>If I then click elsewhere on the page, then back into the textbox and type again, say a 't', I get:</p> <blockquote> <p>LOG: searching for: tes</p> </blockquote> <p>If I click away again then back and type, say "1", I get: </p> <blockquote> <p>LOG: searching for: test</p> </blockquote> <p>I'm sure I've made a silly mistake somewhere. Can anyone tell me how to get the current content of the textbox immediate after a keypress type event being fired, rather than the value prior to the last loss of focus?</p> <p>Thanks Simon</p> <p><strong>edit:</strong><br> As requested, here is the code (from my SearchUtils.js class), including settings the intermediateChanges property to true which solved the problem incase it helps anyone else to refer to it.</p> <pre><code> // Create the Search UI createUI: function (elementName) { // Get a reference to attach the UI to var element = dojo.byId(elementName); if (element) { // Add a text box to enter the search text this.textbox = new dijit.form.TextBox({ id: "txtSearch", intermediateChanges: true }); this.textbox.on("change", lang.hitch(this, this.search)); this.textbox.placeAt(element); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload