javascript - Editing different objects based on the value of a variable -
javascript - Editing different objects based on the value of a variable -
i'm trying alter different variables based on button user has clicked. example, have 3 buttons:
<button id="button1" onclick="isclicked(this.id)">b1</button> <button id="button2" onclick="isclicked(this.id)">b2</button> <button id="button3" onclick="isclicked(this.id)">b3</button>
3 objects:
var button1 = { somevalue: 0, othervalue: 5 }; var button2 = { somevalue: 0, othervalue: 5 }; var button3 = { somevalue: 0, othervalue: 5 };
and id of button clicked passed javascript so
function isclicked(clicked_id) { selectedid = (clicked_id); }
what trying accomplish value applied variable based on button clicked - if button1 clicked, button1 object changed, if button2 clicked, button2 object changed.
i have looked @ programmatically setting name of variable , programmatically set variable names appending id parameter cannot see how work in situation.
is there efficient way this?
nest objects:
var buttons = { button1 : { somevalue: 0, othervalue: 5 }, button2 : { somevalue: 0, othervalue: 5 }, button3 : { somevalue: 0, othervalue: 5 } };
now:
function isclicked(clicked_id) { var theclickedbuttonobject = buttons[clicked_id]; // }
javascript
Comments
Post a Comment