Ampersand
Ampersand

Reputation: 446

jquery/javascript change color of text depending on day

I am trying to make an 'opening hours' table that highlights the current day for the user.

HTML:

                    <div id="Monday"> <h2> Mon</h2><h3>8am-9pm</h3></div>
                    <div id="Tuesday"> <h2> Tue</h2><h3>8am-9pm</h3></div>
                    <div id="Wednesday"> <h2> Wed</h2><h3>8am-9pm</h3></div>
                    <div id="Thursday"> <h2> Thu</h2><h3>8am-9pm</h3></div>
                    <div id="Friday"> <h2> Fri</h2><h3>8am-9pm</h3></div>
                    <div id="Saturday"> <h2> Sat</h2><h3>8am-9pm</h3></div>
                    <div id="Sunday"> <h2> Sun</h2><h3>8am-9pm</h3></div>

jQuery/Javascript/Idon'tevenknowanymore:

var d=newDate();
var day=d.getDay();

if (day == 1)
    {
        document.getElementById('Monday').style.color='#DB35B0'
    }
else if (day == 2)
    {
        document.getElementById('Tuesday').style.color='#DB35B0'
    }
else if (day == 3)
    {
        document.getElementById('Wednesday').style.color='#DB35B0'
    }
else if (day == 4)
    {
        document.getElementById('Thursday').style.color='#DB35B0'
    }
else if (day == 5)
    {
        document.getElementById('Friday').style.color='#DB35B0'
    }
else if (day == 6)
    {
        document.getElementById('Saturday').style.color='#DB35B0'
    }
else if (day == 0)
    {
        document.getElementById('Sunday').style.color='#DB35B0'
    }

The trouble is that if I was going to change the color in the css I would use:

      #Friday h3 {color:#DB35B0;}

which doesn't seem to work in the Javascript like this:

      else if (day == 5)
    {
        document.getElementById('Friday h3').style.color='#DB35B0'
    }

I also tried:

      else if (day == 5)
    {
        document.getElementById('Friday').children.style.color='#DB35B0'
    }

But that did nothing.

So what do I do?

Is it possible to target the children of the divs and change their color?

Upvotes: 3

Views: 4653

Answers (6)

Andy  Gee
Andy Gee

Reputation: 3335

If you're using jquery, this will do it but I'm no expert either!

<style>
.highlighted1{color:#ff0000;}
</style>
<div id="Monday" class="day1"> <h2> Mon</h2><h3>8am-9pm</h3></div>
<div id="Tuesday" class="day2"> <h2> Tue</h2><h3>8am-9pm</h3></div>
<div id="Wednesday" class="day3"> <h2> Wed</h2><h3>8am-9pm</h3></div>
<div id="Thursday" class="day4"> <h2> Thu</h2><h3>8am-9pm</h3></div>
<div id="Friday" class="day5"> <h2> Fri</h2><h3>8am-9pm</h3></div>
<div id="Saturday" class="day6"> <h2> Sat</h2><h3>8am-9pm</h3></div>
<div id="Sunday" class="day0"> <h2> Sun</h2><h3>8am-9pm</h3></div>
<script language="javascript" type="text/javascript">
var day=new Date().getDay();
$(".day"+day+" h3").addClass("highlighted1");
</script>

Upvotes: 1

RobG
RobG

Reputation: 147413

Something like:

var day = (new Date()).getDay();
var ids = ['Sunday','Monday','Tuesday','Wednesday',
           'Thursday','Friday','Saturday'];
var colours = ['#DB35B0','#DB35B0', '#DB35B0',
               '#DB35B0','#DB35B0','#DB35B0','#DB35B0'];

document.getElementById(ids[day]).style.color = colours[day];

Should get you started. Note that the colour array isn't necessary if yo uare setting all the colours to the same value, but maybe you want a different colour for different days.

BTW, you can do this with a class:

<style type="text/css">

  .today h3 {
    color: #DB35B0;
  }

</style>
<script>

function doColours() {
  var ids = ['Sunday','Monday','Tuesday','Wednesday',
             'Thursday','Friday','Saturday'];
  document.getElementById(ids[(new Date()).getDay()]).className = 'today';
}
</script>

Or (heaven forbid!) with document.write:

var day = (new Date()).getDay();
var id = ['Sunday','Monday','Tuesday','Wednesday',
          'Thursday','Friday','Saturday'][day];
var str = '<style type="text/css">'
str += '#' + id + ' h3 { color: #DB35B0; }<\/style>';
document.write(str);

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816552

Yes, there are various methods to retrieve descendant elements (such as getElementsByTagName), but getElementById does not accept a CSS selector. The reason why [...].children.style does not work is that .children returns a NodeList, i.e. a collection of nodes. You'd have to iterate over it or access it with the index of the node you want.

I suggest a different, easier approach:

Create a CSS rule for the color, using a class:

.today h3 {
   color: #DB35B0;
}

Add that class to the current day:

var days = ['Sunday', 'Monday', ..., 'Saturday'],
    now = new Date(),
    today = now.getDay();

document.getElementById(days[today]).className += ' today';

This is less code and you can make changes to the style more easily.

Edit: I just saw that you tagged the question with jQuery. In that case, you can replace the last line with:

$('#' + today).addClass('today');

In contrast to DOM interface methods such as getElementById or getElementsByTagName, jQuery uses CSS selectors to get references to elements. You should have a look at the documentation if you intend to use it.

Upvotes: 2

nnnnnn
nnnnnn

Reputation: 150050

You need to traverse your way through the children. With jQuery it's a one-liner:

$("#Friday h3").css("color","#DB35B0");

In plain JavaScript you have to supply a single id to .getElementById() and it returns a reference to the lone matching element (or it returns null if none matched), but once you have a reference to that element you can process its children:

var div = document.getElementById("Friday"),
    i;

for (i = 0; i < div.children.length; i++)
   div.children[i].style.color = "#DB35B0";

// Or to just get the h3 child elements:

var h3kids = div.getElementsByTagName("h3");
for (i = 0; i < h3kids.length; i++)
   h3kids[i].style.color = "#DB35B0";

// Or, depending on which browsers you want to support (e.g., not IE7)
// you can skip the getElementById step:

var h3list = document.querySelectorAll("#Friday h3");
for (i = 0; i < h3list .length; i++)
   h3list [i].style.color = "#DB35B0";

Upvotes: 0

icyrock.com
icyrock.com

Reputation: 28608

document.getElementById requires you to use the value of the id attribute to get the element. You cannot do document.getElementById('Friday h3'), only document.getElementById('Friday').

Here's a working example:

Using jQuery, you can do this:

$('#Friday h3').css('color', 'red')

Working example:

Upvotes: 0

jfriend00
jfriend00

Reputation: 707476

Since you've tagged jQuery on your question, you could replace all your code with this:

var day = (new Date()).getDay();
var daysOfTheWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

$("#" + daysOfTheWeek[day] + " h3").css("color", '#DB35B0');

or just set a CSS rule for today's color:

.today h3 {color: #DB35B0;}

And, then use this code:

var day = (new Date()).getDay();
var daysOfTheWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

$("#" + daysOfTheWeek[day]).addClass("today");

Upvotes: 2

Related Questions