tags:

views:

104

answers:

4

Here's the updated code again I want the div for Friday, not to appear on Friday. I also want it not to appear after 5pm but I could not get passed day problem. Thank you for all you help.

<html>
<head>

<script type="text/javascript"> 

  $(document).ready(function() {
    var rightNow = new Date(); 
    var day = rightNow.getUTCDay(); 
    if (day == 5) { 
      $('#friday').hide(); 
    } 
  });
</script>

<style type="text/css">

div {
border:solid black 1px;
background-color:lightblue;
color:darkblue;
font-size:14pt;
font-family:arial;
width:550px;
height:220px;
overflow:auto;
padding:5px;
}
</style>


</head>
<body>



<div id='friday'>
friday
</div>
<br>
<div id='saturday'>
saturday
</div>
<br>
<div id='sunday'>
sunday
</div>
<br>



</body>
</html>
+3  A: 

You're assigning instead of comparing:

if (day = 5;) {

Should be:

if (day === 5) {

If you're not familiar with === .. it is the same as == except it is strict (does not perform type coercion).

You can read more about comparison operators here.

Edit:

Also be sure to read John's answer, because it looks like that is contributing to your problem as well.

Matt
I think (day == 5) should be enough.
Hellnar
@Hellnar - in this specific case, yes. But `==` is one of JavaScript's bad parts. `===` is more predictable and it is a good habit to use it by default.
Matt
+7  A: 

Most likely, you're running into an issue where the script executes before the elements are part of the DOM. So, wrap your script like this:

<script type="text/javascript"> 

  $(document).ready(function() {
    var rightNow = new Date(); 
    var day = rightNow.getUTCDay(); 
    if (day == 5) { 
      $('#friday').hide(); 
    } 
  });
</script> 

Notice that I also fixed the "day = 5;" mis-condition.

John Fisher
+1 it looks like he's running his script before the DOM is ready.
Matt
I'm still missing something. I just can't find it.
Ryan
you nailed it, the div isn't there yet, too bad defer isn't supported across the board
curtisk
@Ryan, if you're still having trouble after putting this script into your code, update your post to show the new code. Then, we can see what your new problem is.
John Fisher
<html><head><script type="text/javascript"> $(document).ready(function() { var rightNow = new Date(); var day = rightNow.getUTCDay(); if (day == 5) { $('#friday').hide(); } });</script></head><body><div id='friday'>friday</div><br><div id='saturday'>saturday</div><br><div id='sunday'>sunday</div><br></body></html>
Ryan
Did I not post that code correctly?
Ryan
@Ryan: That new HTML isn't including JQuery, so you would get javascript errors trying to run the jquery code.
John Fisher
@Ryan: You should actually "Edit" the existing question, rather than trying to put code in comments.
John Fisher
I edit the new question and put up code.
Ryan
@Ryan what is still going wrong? Does is work if you replace rightNow.getUTCDay() with a simple 5?
Kathy Van Stone
+1  A: 

i suggest John Fishers answer.

the only thing that you could do to expand on it would be to put the day in a switch, assuming you want to hide each item depending on the day rather than ONLY hide on friday if it's 5

<script type="text/javascript"> 

  $(document).ready(function() {
    var rightNow = new Date(); 
    var day = rightNow.getUTCDay(); 
    switch(day)
    {
      case 0:
        //whatever you want to do on 0
        break;
      case 1:
        //whatever you want to do on 1
        break;
      case 2:
        //whatever you want to do on 2
        break;
      case 3:
        //whatever you want to do on 3
        break;
      case 4:
        //whatever you want to do on 4
        break;
      case 5:
        $('#friday').hide(); 
        break;
      case 6:
        //whatever you want to do on 6
        break;
  });
</script> 
David Larrabee
A: 

The only problem with the latest code you just posted is that you removed your jQuery include (as John Fisher pointed out). Put the line

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;

back in and it will work as you want it to.
To handle the after 5pm problem you would do this:

$(document).ready(function() {
    var rightNow = new Date(); 
    var day = rightNow.getUTCDay();
    if (day == 5 || rightNow.getHours() > 17) { 
      $('#friday').hide(); 
    } 
});

Here's a good reference on the javascript date object.

rosscj2533
Thank you works perfect.
Ryan