Ember.js Component toggleProperty() Method
Last Updated :
26 Apr, 2025
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of websites, including Square, Discourse, Groupon, Linked In, Live Nation, Twitch, and Chipotle.
The toggleProperty() method is used to flip the value of the boolean property to the opposite of its current value.
Syntax:
toggleProperty( keyName );
Parameters:
- keyName: It is the name of the property to toggle.
Return Value: The new property value.
Step 1: To run the following examples you will need to have an ember project with you. To create one, you will need to install ember-cli first. Write the below code in the terminal:
npm install ember-cli
Step 2: Now you can create the project by typing in the following piece of code:
ember new <project-name> --lang en
To start the server, type:
ember serve
Example 1: Type the following code to generate the route for this example:
ember generate route toggleProperty1
app/components/arr.js
Javascript
import Component from '@glimmer/component' ;
import Ember from 'ember' ;
import EmberObject from '@ember/object' ;
import { action, without, set, get } from '@ember/object' ;
const Fruit = EmberObject.extend({
init() {
alert(`${ this .get( 'name' )} is in list Now`);
}
});
export default Ember.Component.extend({
fruits: [
EmberObject.create({
'name' : 'Lady Finger' ,
'isFruit' : false ,
'color' : 'green'
}),
EmberObject.create({
name: 'Brinjal' ,
isFruit: false ,
color: 'purple'
}),
EmberObject.create({
name: 'Apple' ,
isFruit: true ,
color: 'red'
}),
EmberObject.create({
name: 'Grapes' ,
isFruit: true ,
color: 'green'
}),
EmberObject.create({
name: 'Mango' ,
isFruit: true ,
color: 'yellow'
}),
EmberObject.create({
name: 'Watermelon' ,
isFruit: true ,
color: 'red'
}),
EmberObject.create({
name: 'Orange' ,
isFruit: true ,
color: 'orange'
})
],
@action
insertItem(data, data1, data2) {
let temp = Fruit.create({
name: data,
isFruit: data1,
color: data2
});
this .fruits.addObject(temp);
},
@action
ToggleF() {
this .fruits.forEach((item) => item.toggleProperty( 'isFruit' ));
},
})
|
app/components/arr.hbs
HTML
{{page-title "toggleProperty"}}
< h3 >{{yield}}</ h3 >
< table >
< tr >
< th >Name</ th >
< th >Fruit</ th >
< th >Color</ th >
</ tr >
{{#each this.fruits as |detail|}}
< tr >
< td >{{detail.name}}</ td >
< td >{{detail.isFruit}}</ td >
< td >{{detail.color}}</ td >
</ tr >
{{/each}}
</ table >
< br />< br />
< div >
< label >Enter Fruit Name: </ label >
{{input value=this.item1}}
</ div >< br />
< div >
< label >Item is Fruit: </ label >
{{input value=this.item2}}
</ div >< br />
< div >
< label >Enter Fruit color: </ label >
{{input value=this.item3}}
</ div >< br />
< div >
< input
type = "button"
id = "insert-item"
value = "Insert Fruit"
{{action "insertItem" this.item1 this.item2 this.item3}}
/>
</ div >
< br />
< div >
< input
type = "button"
id = "toggle-item"
value = "Toggle Fruit"
{{action "ToggleF" this.item4}}
/>
</ div >
{{outlet}}
|
app/templates/toggleProperty1.hbs
HTML
< Arr >
< h3 >Fruit's List :</ h3 >
</ Arr >
{{outlet}}
|
Output:

toggleProperty output1
Example 2: Type the following code to generate the route for this example:
ember generate route toggleProperty2
app/components/arr2.js
Javascript
import Component from '@glimmer/component' ;
import { tracked } from '@glimmer/tracking' ;
import EmberObject from '@ember/object' ;
import { action } from '@ember/object' ;
import Ember from 'ember' ;
let Student = Ember.Object.extend({
Name: null ,
skill: null ,
id: null ,
init() {
alert(`${ this .get( 'Name' )} is Listed`);
},
fullName: Ember.computed( 'firstName' , 'lastName' , function () {
return `${ this .firstName} ${ this .lastName}`;
}),
Changed: Ember.observer( 'fullName' , function () {
console.log(`fullName changed to: ${ this .fullName}`);
}),
});
export default Ember.Component.extend({
students: [
EmberObject.create({
Name: 'Balit' ,
skill: 'Python' ,
Id: 'stu2' ,
gender: true
}),
EmberObject.create({
Name: 'Yashu' ,
skill: 'PHP' ,
Id: 'stu0' ,
gender: false
}),
EmberObject.create({
Name: 'Sam' ,
skill: 'R' ,
Id: 'stu1' ,
gender: true
}),
EmberObject.create({
Name: 'Pokhu' ,
skill: 'JavaScript' ,
Id: 'stu3' ,
gender: true
}),
EmberObject.create({
Name: 'Tanu' ,
skill: 'Java' ,
Id: 'stu4' ,
gender: false
}),
EmberObject.create({
Name: 'Arabh' ,
skill: 'c++' ,
Id: 'stu5' ,
gender: true
})],
@tracked
item3: 'Gulshan' ,
@tracked
item2: 'Angular' ,
@tracked
item1: 'stu6' ,
@tracked
item4: true ,
@action
print(data1, data2, data3, data4) {
let temp = Student.create({
Name: data1,
skill: data2,
Id: data3,
gender: data4
});
this .students.addObject(temp);
},
@action
toggle() {
this .students.forEach((item) => item.toggleProperty( 'gender' ))
}
})
|
app/components/arr2.hbs
HTML
{{page-title "toggleProperty"}}
< h3 >{{yield}}</ h3 >
< table >
< tr >
< th >Name</ th >
< th >skill</ th >
< th >Id</ th >
< th >Gender</ th >
</ tr >
{{#each this.students as |detail|}}
< tr >
< td >{{detail.Name}}</ td >
< td >{{detail.skill}}</ td >
< td >{{get detail "Id"}}</ td >
< td >{{if detail.gender "Male" "Female"}}</ td >
</ tr >
{{/each}}
</ table >
< br />
< div >
< label >Enter Student FirstName: </ label >
{{input value=this.item3}}
</ div >
< br />
< div >
< label >Enter Student LastName: </ label >
{{input value=this.item2}}
</ div >
< br />
< div >
< label >Enter Student Id: </ label >
{{input value=this.item1}}
</ div >
< br />
< div >
< label >Enter Student Gender: </ label >
{{input value=this.item4}}
</ div >
< br />
< input
type = "button"
id = "set-code"
value = "Update Student details"
{{action "print" this.item3 this.item2 this.item1 this.item4}}
/>
< br />
< br />
< br />
< input
type = "button"
id = "toggle-student"
value = "Toggle Gender"
{{action "toggle"}}
/>
|
app/routes/toggleProperty2.js
HTML
< Arr2 >
List of Students:
</ Arr2 >
{{outlet}}
|
Output:

toggleProperty output2
Reference: https://api.emberjs.com/ember/4.6/classes/Component/methods/willDestroy?anchor=toggleProperty
Similar Reads
React Suite Toggle Component
React Suite is a popular front-end library with a set of React components that are designed for the middle platform and back-end products. The toggle component allows the user to select between two values. We can use the following approach in ReactJS to use the React Suite Toggle Component. Toggle P
2 min read
p5.js Element toggleClass() Method
The toggleClass() of p5.Element in p5.js is used to toggle the specified class in the element. The toggling of a class means that it would be added or removed depending on the current state. Syntax: toggleClass(class) Parameters: This function accepts a single parameter as mentioned above and descri
2 min read
JavaScript Property Accessors Method
Property Accessors allow access with the property name or keys of an object (Reading, Creating, Updating). There are two notations in JavaScript that allows us to access object's properties: Dot NotationBracket Notation [ ] If the object does not find a matching key(or property name or method name),
1 min read
HTML DOM toggleAttribute() Method
The toggleAttribute() method of the element interface toggles a Boolean attribute on the given element. Attributes of an element can be changed using this method. Syntax: Element.toggleAttribute("attribute_name"); Parameters: Name_of_attribute: Name of the attribute to be toggled. The attribute name
1 min read
Angular PrimeNG Form ToggleButton Labels Component
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will know how to use the ToggleButton Labels Component in Angular PrimeNG. The T
3 min read
Angular PrimeNG ToggleButton Component
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will know how to use the ToggleButton component in Angular PrimeNG. ToggleButton
4 min read
How to use toggle() method in jQuery ?
The toggle() method is used to oscillate between the properties of CSS while used to produce the animation effect to the elements. The various jQuery effects are hide(), fade(), and slide(). Basically, the toggle() method oscillates between the CSS property display: none if it is present else it get
1 min read
Angular PrimeNG Form ToggleButton Icons Component
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will know how to use the ToggleButton Icons Component in Angular PrimeNG. The To
3 min read
Bootstrap 5 Tooltips toggle() Method
Bootstrap 5 Tooltips toggle() method is used to toggle a tooltip's visibility manually. Calling it an odd number of times on a tooltip makes it visible, and calling it an even number of times makes it hidden. Syntax:tooltip.toggle()Return Value: The user receives a direct response from this method
2 min read
jQuery toggle() Method
The toggle() method is used to check the visibility of selected elements to toggle between hide() and show() for the selected elements. show() is run when the element is hidden. hide() is run when the element is visible. Syntax: $(selector).toggle(speed, easing, callback) Parameters: It has three op
1 min read