www.webdeveloper.com
Results 1 to 5 of 5

Thread: Slight error with code?

  1. #1
    Join Date
    Oct 2012
    Posts
    16

    Slight error with code?

    Hi guys,

    I wrote the below code to look into a div to find its content (which is March), then loop through a months array to find a match of the div content.

    It's not working for some reason, any idea why? If I change the

    Code:
    var current_month = document.getElementById('month');
    to


    Code:
    var current_month = "March";
    it works fine but it just won't recognise the word March in the div...

    Any help would be great.

    Code:
    $('#arrow-right').click(function () {  var current_month = document.getElementById('month');  var i;  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];  for (i=0;i<months.length;i++) {  if (current_month == months[i]) {     months[i++];    var next_month = months[i];    alert(next_month);    break;  }  // Code to be added later  }  });

  2. #2
    Join Date
    Oct 2012
    Posts
    16
    Just updated to:

    Code:
    var current_month = document.getElementById('month').innerHTML;
    which is collecting March but it still won't match the array :-(

  3. #3
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    if you are using jQuery, use its built-in tools:

    Code:
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title>jQuery.inArray(value,array)</title>
    <style>
    body{text-align:center;padding-top:250px;}
    input{text-align:center;}
    #result{margin-top:15px;margin-bottom:15px;}
    </style>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
    var months=["January","February","March","April","May","June","July","August","September","October","November","December"];
    $(document).ready(function(){
    
    $('#arrow-right').click(function(){
    var current_month=$('#month').val(),
    	index=jQuery.inArray(current_month,months),
    	next_month='';
    if(index!=-1){
    next_month=(index==months.length-1)?months[0]:months[index+1];
    // Code to be added later
    }
    else{next_month='not found';}
    $('#result').html(next_month);	
    });
    
    });
    </script>
    </head>
    <body>
    <h3>Type in the month you like and click the button below:</h3><input type="text" id="month" value="March" />
    <div id="result"></div>
    <input type="button" id="arrow-right" value="Check it" />
    </body>
    </html>

  4. #4
    Join Date
    Oct 2012
    Posts
    16
    Very nice, thank you!

  5. #5
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    anytime )

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles