jquery - How can I change text content every day with javascript? -
i making sort of script show different content everyday different image , title. got image loop show every day different picture need same title.
it making dynamic page of discount deals different every day repeats after 1 week need have 7 pictures , 7 titles show.
here code loop of images every day.
var dailyphotos; var today, img; dailyphotos = function() { today = new date(); weekday = today.getday(); showimages = [ ]; myphotos = [ "{{root}}assets/img/sunday.jpg", "{{root}}assets/img/monday.jpg", "{{root}}assets/img/tuesday.jpg", "{{root}}assets/img/wednesday.jpg", "{{root}}assets/img/thirsday.png", "{{root}}assets/img/friday.jpg", "{{root}}assets/img/saterday.jpg" ]; // must specify path or file name of images loaded in weekday basis. if ( document.images ) { ( var x = 0; x < myphotos.length; x++ ) { showimages[ x ] = new image(); showimages[ x ].src = myphotos[ x ]; } img = (( document.getelementbyid ) ? document.getelementbyid("yourimageid") : document.images.yourimageid ); // specify id of image raplaced daily. img.src = showimages[ weekday ].src; img.alt = myphotos[ weekday ]; } return false; // if browser can't display images, exit function. }; window.onload = dailyphotos;
and how want display in html
<div class="row"> <div class="column large-6 medium-6 small-12"> <h4> want title displayed </h4> <p> if can display p tekst have in here </p> <a href="more details medium-6 small-12"></a> </div> <div class="column large-6"><img id="yourimageid" src="tuesdayphoto.jpg" alt="demo" /> image displayed </div> </div>
i know can done javascript, trying if reading this.
var dailyphotos; var today, img; dailyphotos = function() { today = new date(); weekday = today.getday(); showimages = [ ]; myphotos = [ "{{root}}assets/img/sunday.jpg", "{{root}}assets/img/monday.jpg", "{{root}}assets/img/tuesday.jpg", "{{root}}assets/img/wednesday.jpg", "http://s.cdpn.io/37045/wedding-1.jpg", "{{root}}assets/img/friday.jpg", "{{root}}assets/img/saterday.jpg" ]; // must specify path or file name of images loaded in weekday basis. if ( document.images ) { ( var x = 0; x < myphotos.length; x++ ) { showimages[ x ] = new image(); showimages[ x ].src = myphotos[ x ]; } img = (( document.getelementbyid ) ? document.getelementbyid("yourimageid") : document.images.yourimageid ); // specify id of image raplaced daily. img.src = showimages[ weekday ].src; img.alt = myphotos[ weekday ]; } return false; // if browser can't display images, exit function. }; window.onload = dailyphotos;
<div class="row"> <div class="column large-6 medium-6 small-12"> <h4> want title displayed </h4> <p> if can display p tekst have in here </p> <a href="more details medium-6 small-12"></a> </div> <div class="column large-6"> <img id="yourimageid" src="tuesdayphoto.jpg" alt="demo" /> image displahttp://stackoverflow.com/posts/43251456/edit#yed </div> </div>
here go!
https://jsfiddle.net/bja94ulz/1/
first date
var d = new date();
then day
var n = d.getday();
then can make array titles. doesn't need below above
var titles = ['title 1', 'title 2', 'title 3', 'title 4', 'title 5', 'title 6', 'title 7'];
and change title replacing content in div id title
document.getelementbyid("title").innerhtml = titles[n-1];
it's 'n-1' because arrays start @ 0
you can away using 1 less variable. https://jsfiddle.net/bja94ulz/2/
Comments
Post a Comment