[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem with adding 1 to a number in a each function

Posted on 2014-08-24
10
Medium Priority
?
199 Views
Last Modified: 2014-08-29
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?
0
Comment
Question by:Panos
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40282072
If tmpcnt is a variable then it has no value attribute (tmpcnt.value)
0
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 40282077
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
 
LVL 2

Author Comment

by:Panos
ID: 40282086
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40282111
Is there some HTML associated with this question?  If so, could you please post the HTML as well as the JavaScript (jQuery)?  Thanks.
0
 
LVL 2

Assisted Solution

by:Panos
Panos earned 0 total points
ID: 40282113
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
 
LVL 83

Expert Comment

by:leakim971
ID: 40282166
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40282185
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
 
LVL 2

Author Comment

by:Panos
ID: 40282539
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
 
LVL 2

Author Closing Comment

by:Panos
ID: 40292158
Thank you very much
regards
panos
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question