Knockout JS Interview Questions and Answers
Ques 11. What is the purpose of the 'attr' binding in Knockout JS?
The 'attr' binding is used to set or remove one or more attributes of an HTML element based on the value of the associated observable or expression.
Example:
; var viewModel = { imageUrl: 'path/to/image.jpg', imageAlt: 'Image Alt Text' };
Ques 12. How can you handle key events in Knockout JS?
You can use the 'event' binding to handle various events, including key events, on HTML elements.
Example:
; var viewModel = { handleKeyPress: function(data, event) { console.log('Key pressed:', event.key); } };
Ques 13. Explain the 'css' binding in Knockout JS.
The 'css' binding is used to apply or remove CSS classes to an HTML element based on the truthiness of the associated observable or expression.
Example:
Content; var viewModel = { isActive: ko.observable(true), isDisabled: ko.observable(false) };
Ques 14. What is the purpose of the 'value' binding in Knockout JS?
The 'value' binding is used to bind an input, select, or textarea element's value to an observable, allowing two-way data binding.
Example:
; var viewModel = { userInput: ko.observable('Initial value') };
Ques 15. Explain the concept of custom bindings in Knockout JS.
Custom bindings in Knockout JS allow you to create reusable, encapsulated behaviors for handling specific aspects of the UI.
Example:
ko.bindingHandlers.fadeVisible = { init: function(element, valueAccessor) { var value = valueAccessor(); $(element).toggle(ko.unwrap(value)); }, update: function(element, valueAccessor) { var value = valueAccessor(); ko.unwrap(value) ? $(element).fadeIn() : $(element).fadeOut(); } };
Most helpful rated by users: