I have an array with 3 objects
map.la = [object, object, object];
I want to create a button for each of these objects. The reason i have to dybamicaly create the buttons is because the number of objects varies from time to time.
How can i loop through each object and create a button for it and then set the tooltip of the button to equal the name property inside each object?
the button:
this._selectionbutt = new SegButton({
items: [
//each of the below items (this.selection) should be added here to items array. because we have 3 buttons there
//should be three unique items here. the items should have the same name as the variable we instantiate the
//button with.
this._oSelection
]
});
this.selection = new SegButtItems({
icon: "map",
tooltip: //this should be the name property of each item
press: this._handleSelection();
});
Once the user selects the button any of the buttons they will be taken to the same function (this._handleSelection()) and this function will need to check the name property string eg "map333" and thats it.
Any guidance will be highly appreicated :)
Thank you
Here is a simple example:
HTML code. Create div, to wich we will add buttons
<div id="buttons"></div>
Js code:
//create objects
var Object1 = {
icon: "map",
tooltip: "Press Me",
press: 'console.log("Button1 pressed")'
};
var Object2 = {
icon: "map",
tooltip: "Press Me",
press: 'console.log("Button2 pressed")'
};
var Object3 = {
icon: "map",
tooltip: "Press Me",
press: 'console.log("Button3 pressed")'
};
//add objects to array
var ar = [Object1, Object2, Object3];
//add buttons for every object in array
ar.forEach( function(v) {
var button= document.createElement('button');
button.type= 'button';
button.appendChild(document.createTextNode(v.tooltip));
button.setAttribute('onClick',v.press);
document.getElementById("buttons").appendChild(button);
} );
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.