0

Okay. This might be a little bit difficult to explain. Let's just say I have three variables in JavaScript

window.seconds = 0;
window.minutes = 0;
window.hours   = 0;

and also three anchor tags in HTML

<a id='seconds_plus' href=''>Increase seconds</a><br>
<a id='minutes_plus' href=''>Increase minutes</a><br>
<a id='hours_plus'   href=''>Increase hours</a><br>

Each time a anchor is clicked, the integer of the variable will increase by 1. Now see, I am a lazy person and don't want to write 3 same .click() functions. That is why I came up with a single .click() function on which I have a problem on.

$("#hours_plus, #minutes_plus, #seconds_plus").click(function () {
    var increase = $(this).attr("id").split("_");
    increase[0]++; // <-----
});

Is there a way this could be possible to do?

1 Answer 1

2

Just change

 increase[0]++

to

window[increase[0]]++
Sign up to request clarification or add additional context in comments.

1 Comment

In addition, I would use a distinct attribute, like <a id='seconds_plus' data-type="seconds" then you'd use var increase = $(this).data('type'); instead of splitting the id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.