Note that there are some explanatory texts on larger screens.

plurals
  1. POHTML5 Contenteditable field and knockout: event rises before value binding
    primarykey
    data
    text
    <p>I'm trying to to display an editable field bound to an observable. I use knockout plus 'contenteditable' property (html5). After editing the field, its value should be posted to server as a json string.</p> <p>The problem is with the value being posted to server. I expect to upload newly edited value, but instead of it I get old value.</p> <p>Here is the illustration of the issue on jsfiddle: <a href="http://jsfiddle.net/N8LDH/15/" rel="nofollow">link</a></p> <p>View:</p> <pre><code>&lt;h2 contenteditable="true" data-bind="editableText: title, event: { change: valueChanged }"&gt;&lt;/h2&gt; </code></pre> <p>editableText binder:</p> <pre><code>ko.bindingHandlers.editableText = { init: function (element, valueAccessor) { $(element).on('blur', function () { var observable = valueAccessor(); observable($(this).text()); }); }, update: function (element, valueAccessor) { var value = ko.utils.unwrapObservable(valueAccessor()); $(element).text(value); }}; </code></pre> <p>'Change' Event Trigger:</p> <pre><code> $('[contenteditable]').live('focus', function() { var $this = $(this); $this.data('before', $this.html()); return $this; }).live('blur paste', function() { var $this = $(this); if ($this.data('before') !== $this.html()) { $this.data('before', $this.html()); $this.trigger('change'); } return $this; }).live('keypress', function(e) { var code = (e.keyCode ? e.keyCode : e.which); if (code == 13) { var $this = $(this); if ($this.data('before') !== $this.html()) { $this.data('before', $this.html()); $this.trigger('change'); } e.preventDefault(); } return $this; }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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