0

In my novice understanding of jQuery, this code should input the sunset time into the span div. But it does not work. Any tips?

HTML

<div id="sunsetbox">Sunset tonight at<span></span></div>

JQuery

$(function(){
    var month = getMonth()+1;
    var day = getDate();
    var todaysdate = (month+'/'+day+'/');   
    var sunsettimes = [10/1,"7:19",10/2,"7:17",10/3,"7:15"];
    var index = sunsettimes.indexOf(todaysdate);
    var indexsunset = index + 1;
    var displaysunsettime = sunsettimes.slice(indexsunset);
    $("#sunsetbox span").html(displaysunsettime);
});
6
  • 1
    after day there is no / in the array value Commented Oct 1, 2014 at 16:58
  • Good eye, fixed that but still is not working.. Commented Oct 1, 2014 at 17:01
  • Do you create date object anywhere? You can't use methods like getMonth() on their own. Commented Oct 1, 2014 at 17:02
  • I didn't know you had to add a date object, but after adding it like @ArunPJohny did, still is not working for me. Commented Oct 1, 2014 at 17:08
  • Just updated things one more time to make sure, this time it worked! Thanks! Commented Oct 1, 2014 at 17:10

4 Answers 4

1

Try

$(function() {
  var date = new Date();
  //need to use a date object
  var month = date.getMonth() + 1;
  var day = date.getDate();
  //there is no / after day
  var todaysdate = month + '/' + day;
  var sunsettimes = ['10/1', "7:19", '10/2', "7:17", '10/3', "7:15"];
  var index = sunsettimes.indexOf(todaysdate);
  var indexsunset = index + 1;
  //need to get the value at index indexsunset
  var displaysunsettime = sunsettimes[indexsunset];

  $("#sunsetbox span").html(displaysunsettime);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="sunsetbox">Sunset tonight at <span></span>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

Many mistakes in the code, this code should do what you wanted.

$(function(){

    var d = new Date();
    var month = d.getMonth()+1;
    var day = d.getDate();
    var todaysdate = month+'/'+day;   
    var sunsettimes = ["10/1","7:19","10/2","7:17","10/3","7:15"];
    var index = sunsettimes.indexOf(todaysdate);
    var indexsunset = index + 1;
    var displaysunsettime = sunsettimes[indexsunset];
    $("#sunsetbox span").html(displaysunsettime);
});

Comments

0

The dates in sunsettimes need to be Strings. ("10/1", not 10/1) Right now, you are performing a divide operator on 10 & 1, so the value of your index var is getting set to -1 (not found).

Comments

0

I think this line :

var sunsettimes = [10/1,"7:19",10/2,"7:17",10/3,"7:15"];

is the main problem as the dates you put in are interpreted as operations (for the first one, 10 divided by 1. Changin the line to:

var sunsettimes = ['10/1',"7:19",'10/2',"7:17",'10/3',"7:15"];

So the dates are used as Strings should do the trick.

Also, as you said you're new to this, I'll allow myself to give you some advice/knowledge. The system you use (a list with both the keys and the values side by side) seems weird as there are already some things taking of this problem perfectly in Javascript. Those things are the Objects.

This is how you would use them:

$(function() {
  var date = new Date();
  //need to use a date object
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var todaysdate = month + '/' + day;
  var sunsettimes = {
    '10/1': "7:19",
    '10/2': "7:17",
    '10/3': "7:15"
  };
  //need to get the value at index indexsunset
  var displaysunsettime = sunsettimes[todaysdate];

  $("#sunsetbox span").html(displaysunsettime);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="sunsetbox">Sunset tonight at <span></span>
</div>

The objects allow you associate a value to a key, and they will return the value associated to a key you pass them.

I hope i was clear enough

Comments

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.