By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,777 Members | 1,311 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,777 IT Pros & Developers. It's quick & easy.

next and previous function not defined error am trying to fix it but couldn't please

P: 1
Expand|Select|Wrap|Line Numbers
  1. <html> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title></title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" media="screen" href="https://bytes.com/calender.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <script src="https://bytes.com/calender.js" type="text/javascript"></script> </head> <body onload="initialise();"> <button onclick="previous();"> <i class="fa fa-angle-down"></i></button> <span id="thismonth"></span> <button onclick="next();"> <i class="fa fa-angle-up"></i></button> </body> <script>
  2. var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
  3. var currentMonth=0;
  4. var currentYear=0;
  5. var d= new Date();
  6. currentMonth=d.getMonth();
  7. currentYear=d.getFullYear();
  8. var firstdate=months[currentMonth]+ " " + 1 + " " + currentYear;
  9. function initialise(){
  10.         document.getElementById("thismonth").innerHTML=months[currentMonth]+ "  " + currentYear;
  11. function previous(){
  12.     if(currentMonth===0){
  13.  
  14.         currentMonth=11;
  15.         currentYear=currentYear-1;
  16.     }
  17.     else if(currentMonth>0){
  18.         currentMonth=currentMonth-1;
  19.  }
  20.  document.getElementById("thismonth").innerHTML=months[currentMonth]+ "  " + currentYear;
  21. }
  22.  
  23.  
  24. function next(){
  25.     if(currentMonth<11){
  26.  
  27.         currentMonth=currentMonth+1;
  28.     }
  29.     else if(currentMonth === 11){
  30.         currentMonth=0;
  31.         currentYear=currentYear+1;     
  32.     }
  33.     document.getElementById("thismonth").innerHTML=months[currentMonth]+ " " +currentYear;
  34. }
  35. }
  36. </script> </html>
Oct 19 '18 #1
Share this Question
Share on Google+
1 Reply


gits
Expert Mod 5K+
P: 5,328
have a close look at what you did construct. you are declaring the functions next and previous in the scope of the function initialise - thus they are not available in the window scope from where you want to call them in your onclick-handlers.

a hacky and not the preferred way to fix that could be to just add something like this:

Expand|Select|Wrap|Line Numbers
  1. window.previous = function previous() { ...
which would bind the function to the window scope with the name previous so it could be called as you did.

The better way would be to assign the handler during the initialise-operation (instead of polluting the window scope like in the above example). There are different ways to do it like:

Expand|Select|Wrap|Line Numbers
  1. node.onclick = function() { //code here };
or:

Expand|Select|Wrap|Line Numbers
  1. node.addEventListener('click', function() { //code here }, true || false);
instead of writing the functions like in that pseudo-code above it would be more elegant to declare them before and assign/pass them by name only.
Oct 19 '18 #2

Post your reply

Sign in to post your reply or Sign up for a free account.