Problem with adding 1 to a number in a each function

Hello experts.
I have a problem with the following code:
var tmpcnt = 0;
alert(tmpcnt);
$.each(opt, function(index, value) {
  alert(tmpcnt);
  var cnt = parseInt(tmpcnt.value) + 1;
  alert(cnt);
  if (cnt == 3) {
    var tmpcnt = 0;
  } else {}
});
In the first alert i get 0 but the other alerts either have nothing ( the second) or i get a js error:tmpcnt is undefined
Any help?
LVL 2
PanosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
If tmpcnt is a variable then it has no value attribute (tmpcnt.value)
0
leakim971PluritechnicianCommented:
work :
var tmpcnt = 0;
$.each(opt, function(index, value) {
    var cnt = ++tmpcnt;
    alert(cnt);
    if(cnt == 3) {
        tmpcnt = 0;
    }
});

Open in new window

doesn't work:
var tmpcnt = 0;
$.each(opt, function(index, value) {
    var cnt = ++tmpcnt;
    alert(cnt);
    if(cnt == 3) {
        var tmpcnt = 0;
    }
});

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PanosAuthor Commented:
Hi thank you for your posts
leakim971  isn't work and doesn't work the same?

I get first alert 0 and the others NaN
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ray PaseurCommented:
Is there some HTML associated with this question?  If so, could you please post the HTML as well as the JavaScript (jQuery)?  Thanks.
0
PanosAuthor Commented:
Hi.
I have a working solution. The html was not needed. Thank you
var cnt = 0;
$.each(opt, function(index, value) {
  cnt += 1;
  if (cnt == 3) {
     cnt = 0 ;
  } else {}
});

Open in new window

0
leakim971PluritechnicianCommented:
0
Ray PaseurCommented:
The point of asking for the HTML was to be able to visualize the solution.  It's not "needed" at one level, but it's useful to anyone who might come upon this question in the future.  With the HTML + the jQuery, they might be able to copy / paste the code snippet and see it work.
0
PanosAuthor Commented:
leakim971
the code is really working. I realized now the differences between  the work and doesn't work. Sorry i made it difficult for you

Ray Paseur
To post an html code i have to create one. This code is part of a very large function.
The problem is not to show the function but to find the mistake in the code. That's why i made it as simple as possible for the experts deleting all unnecessary code.
0
PanosAuthor Commented:
Thank you very much
regards
panos
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.