1

I've been toying around with getDay() and HTML5 Data Attributes to try and display a message similar to Basecamp's footer.

"Enjoy the rest of your Sunday!" https://basecamp.com/

I've managed to achieve returning the day of the week using jquery, but not sure how to apply that to the data attribute.

Would love some assistance. Thanks.

dcpomfret
  • 105
  • 1
  • 1
  • 5

1 Answers1

2

Suppose you have something like this in html code

<p>Enjoy the rest of your <span data-day="wday"></span>!</p>

Define a javascript function like so :

function dayOfWeekAsString(idx) {
      return ["Sunday", "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][idx];
    }

Then you can just do

var d=new Date();
var day_idx = d.getDay();
var week_day = dayOfWeekAsString(day_idx);
$("p").find("[data-day='wday']").html(week_day);

EDIT: Updated code with Jsfiddle link here

function dayOfWeekAsString(idx) {
  return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][idx];
}


var d = new Date();
var day_idx = d.getDay();
var week_day = dayOfWeekAsString(day_idx);
$("p").find("[data-day='wday']").html(week_day);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Enjoy the rest of your <span data-day="wday"></span>!</p>
Marcel Djaman
  • 1,276
  • 1
  • 17
  • 34