0

I want to write a code which will add 1 to the object value on button click where button ID is the same as name in object (I will write function to dynamically add new positions in object). I hope you understand what I am talking about. I'm not English native speaker.. :)

I have this code (in first alert i want to get "99" and "100" in second but it doesn't work when I use this.id):

var obj = {mybuttonid: 99};
$('button').click(function () {
alert(obj.this.id);
obj.this.id++;
alert(obj.this.id);
});
CGeek
  • 25
  • 4

1 Answers1

2

If I understood correctly, you want

var obj = {mybuttonid: 99};
$('button').click(function () {
    alert(obj[this.id]);
    obj[this.id]++;
    alert(obj[this.id]);
});

var obj = {
  "foo": 99,
  "bar": 50
};
$('button').click(function () {
  alert(obj[this.id]);
  obj[this.id]++;
  alert(obj[this.id]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="foo">99</button>
<button id="bar">50</button>
Oriol
  • 274,082
  • 63
  • 437
  • 513