I am trying to get text entered in an input box to display in the console, but I'm not sure what I'm doing wrong. The error that comes up says "Uncaught ReferenceError: enteredText is not defined". I am using knockout.
HTML
<input id="searchbox" type="text" placeholder="Search" data-bind="value: enteredText, valueUpdate: 'keyup'" />
JavaScript
var viewModel = {
enteredText: ko.observable("")
};
ko.applyBindings(viewModel);
console.log(viewModel.enteredText);
If you want to see the update in real time, you can subscribe to the observable and then log every time it changes:
var viewModel = { enteredText: ko.observable("") }; viewModel.enteredText.subscribe(function(newValue){ console.log(viewModel.enteredText()); }); ko.applyBindings(viewModel);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <input id="searchbox" type="text" placeholder="Search" data-bind="value: enteredText, valueUpdate: 'keyup'" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.