
- KnockoutJS Tutorial
- KnockoutJS - Home
- KnockoutJS - Overview
- KnockoutJS - Environment Setup
- KnockoutJS - Application
- KnockoutJS - MVVM Framework
- KnockoutJS - Observables
- Computed Observables
- KnockoutJS - Declarative Bindings
- KnockoutJS - Dependency Tracking
- KnockoutJS - Templating
- KnockoutJS - Components
- KnockoutJS Resources
- KnockoutJS - Quick Guide
- KnockoutJS - Resources
- KnockoutJS - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
KnockoutJS - push() Method
Description
The KnockoutJS Observable push('value') method inserts a new item at the end of an array.
Syntax
arrayName.push('value')
Parameters
Accepts only one parameter, that is the value to be inserted.
Example
<!DOCTYPE html> <head> <title>KnockoutJS Observable Array push() Method</title> <script src = "https://ajax.aspnetcdn.com/ajax/knockout/knockout-3.1.0.js" type = "text/javascript"></script> </head> <body> <p>Example to demonstrate push() method.</p> <p>Enter name: <input data-bind = 'value: empName' /></p> <p><button data-bind = "click: addEmp">Add Emp </button></p> <p>Array of employees: <span data-bind = "text: empArray()" ></span></p> <script> function EmployeeModel() { this.empName = ko.observable(""); this.chosenItem = ko.observableArray(""); this.empArray = ko.observableArray(['Scott','James','Jordan','Lee', 'RoseMary','Kathie']); //Initial Values this.addEmp = function() { if (this.empName() != "") { this.empArray.push(this.empName()); //insert accepted value in array this.empName(""); } }.bind(this); } var emp = new EmployeeModel(); ko.applyBindings(emp); </script> </body> </html>
Output
Let's carry out the following steps to see how the above code works −
Save the above code in array-push.htm file.
Open this HTML file in a browser.
Type 'Tom' as an input and click the Add Emp button.
knockoutjs_observables.htm
Advertisements