www.webdeveloper.com
Results 1 to 3 of 3

Thread: Hiding a <div> based on a specific date...

  1. #1
    Join Date
    Dec 2012
    Posts
    81

    Hiding a <div> based on a specific date...

    I thought I'd post this script here. Figured this out after doing some trial and error.

    Have you ever wanted to have something disappear from your site after a certain date? This script will do just that.

    You can follow the link here to see it in action: http://webfro.gs/scripts/js/timing_device/. I have also posted a link to download a demo with all the necessary files.

    Here is the code...

    The CSS

    Code:
    .DateRange, .DateDiv { display: none; text-align: center; }
    The JavaScript

    Code:
    $(function() {
        $(".DateDiv").each(function(index) {
            var sRange = $(this).find(".DateRange").html();
            var arrTemp = sRange.split(" to ");
            var dtFrom = new Date(arrTemp[0]);
            var dtTo = new Date(arrTemp[1]);
            var dtNow = new Date();
            if (dtNow >= dtFrom && dtNow <= dtTo)
                $(this).show();
        });
    });
    The HTML

    HTML Code:
    <h2>The following image will disappear on August 2, 2013, at midnight:</h2>
    <br />
    <br />
    
    <div class="DateDiv">
    	<span class="DateRange">8/2/2013 to 8/3/2013</span>
        	<img src="img/button_home.jpg" width="250" height="252">
            <p>This can be text, too</p>
    </div>
    Here's another sample of it on the web that I built. Click here(jsfiddle).


    Regards,
    ____________________________________
    sƃoɹɟqǝʍ Design Studios
    Phoenix | Arizona

    E: webfrogs@webfro.gs

  2. #2
    Join Date
    Dec 2005
    Location
    TX
    Posts
    7,946

    Smile

    Here's another way to do it without using JQuery.

    Added code to display image change on daily basis
    AND/OR
    change image on same day (holiday) of the year.

    Note, you can add logic to display on a specific year only if desired,
    but I thought it would be more useful as a repeating display.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Daily Displays </title>
    <style type="text/css">
     #DailyImage .DayImage { display:none; }
     #DailyImage div img { height:125px; width:100px; }
     #HolidayImages .DayImage { display:none; }
    </style>
    
    </head>
    <body>
    <h2>The following image will change each day of the week.</h2>
    
    <div id="DailyImage">
     <div id="Day0" class="DayImage">
      <span class="DateRange"></span>   <img id="imgDay0" src="" alt="" title="">
      <p>This can be text for Sunday too</p>
     </div>
     <div id="Day1" class="DayImage">
      <span class="DateRange"></span>   <img id="imgDay1" src="" alt="" title="">
      <p>This can be text for Monday too</p>
     </div>
     <div id="Day2" class="DayImage">
      <span class="DateRange"></span>   <img id="imgDay2" src="" alt="" title="">
      <p>This can be text for Tuesday too</p>
     </div>
     <div id="Day3" class="DayImage">
      <span class="DateRange"></span>   <img id="imgDay3" src="" alt="" title="">
      <p>This can be text for Wednesday too</p>
     </div>
     <div id="Day4" class="DayImage">
      <span class="DateRange"></span>   <img id="imgDay4" src="" alt="" title="">
      <p>This can be text for Thursday too</p>
     </div>
     <div id="Day5" class="DayImage">
      <span class="DateRange"></span>   <img id="imgDay5" src="" alt="" title="">
      <p>This can be text for Friday too</p>
     </div>
     <div id="Day6" class="DayImage">
      <span class="DateRange"></span>   <img id="imgDay6" src="" alt="" title="">
      <p>This can be text for Saturday too</p>
     </div>
    </div>
    <p>&nbsp;</p>
    
    <h2>The following image will change specific defined holidays.</h2>
    
    <div id="HolidayImages">
     <div id="Day_8_2" class="DayImage">
      <span>Aug. 2 to Aug. 3</span>
      <img src="http://www.nova.edu/hpd/otm/pics/4fun/Taz.JPG" height="125" width="100">
      <p>This can be text for holiday on MONTH, XX of each year</p>
     </div>
     <div id="Day_8_3" class="DayImage">
      <span>Aug. 3 to Aug. 4</span>
      <img src="http://www.nova.edu/hpd/otm/pics/4fun/BuzzLite.JPG" height="125" width="100">
      <p>This can be text for holiday on MONTH, XX of each year</p>
     </div>
    </div>
    
    <script type="text/javascript">
    
    var baseURL = "http://www.nova.edu/hpd/otm/pics/4fun/";
    var imgList = ['11.jpg','12.jpg','13.jpg','14.jpg','15.jpg','21.jpg','22.jpg'];
    var DOW = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    
    window.onload = function() {
      for (var d=0; d<imgList.length; d++) {
        document.getElementById('imgDay'+d).src = baseURL+imgList[d];
        document.getElementById('imgDay'+d).alt = imgList[d];
        document.getElementById('imgDay'+d).title = imgList[d];
      }
      var sel = document.getElementById('DailyImage').getElementsByClassName('DateRange');
      for (var i=0; i<sel.length; i++) { sel[i].innerHTML = DOW[i]+ "'s mood"; }
    
      var today = new Date();
      var day = today.getDay(); 
      document.getElementById('Day'+day).style.display = 'block';
    
    // end of daily change display and start of holiday displays
    
      var month = '_'+(today.getMonth()+1);
      var date = '_'+today.getDate();
      var temp = 'Day'+month+date;  
      if (document.getElementById(temp)) { document.getElementById(temp).style.display = 'block'; }
    }
    
    </script>
    
    </body>
    </html>
    Last edited by JMRKER; 08-02-2013 at 08:53 PM.

  3. #3
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    585
    This is my entry:
    Code:
    function eventTable(eYr,eMo,eDa,eText) {  
      this.date = new Date(eYr, eMo-1, eDa, 15, 0);  // Time set at 3pm. In JavaScript, months are from 0 (Jan) to 11 (Dec) so 1 is subtracted.
      var wkText = ""
      for (var i=0; i < eText.length; i++) {
        wkText+= eText[i] + "<br>"; }
      this.text = wkText;
    }
    ETable = new Array; // Table entries MUST start from zero and be in chronological order.  When you delete the first one all other entres must be renumbered.
    ETable[0] = new eventTable(2013,2,24,[["Orchestra Showcase"],["Overture to \"Iphigenia in Aulis\" by Christoph Willibald von Gluck"],[""],
    ["Summer from \"The Four Seasons\" by Antonio Vivaldi"],["Julia Kirk - violin soloist"],[""],
    ["\"Concerto in Eb Major\" by Jan Krtitel Jiri Neruda"],["Anne Berthold - trumpet soloist"],[""],
    ["\"Concerto for Oboe\" by Vincenzo Bellini"],["Wayne Miller - oboe soloist"],[""],
    ["\"Der Rosenkavalier Waltzes\" by Richard Strauss (arr. O. Singer, orch. J. Doebber)"]]);
    ETable[1] = new eventTable(2013,11,10,[["The program hasn't been determined."],["Please check later."]]);
    ETable[2] = new eventTable(2014,2,23,[["The program hasn't been determined."],["Please check later."]]);
    ETable[3] = new eventTable(2014,4,1,[["Testing Entry"],[""],
    ["1712 Overture by P.D.Q. Bach"],[""],
    ["Iphigenia in Brooklyn by PDQ Bach"],["\"Annual April Fool\" Concert"]]);
    ETable[4] = new eventTable(2014,5,4,[["The program hasn't been determined."],["Please check later."]]);
    and it's used here:
    Code:
    function readTable() {
    var months = [["January"],["February"],["March"],["April"],["May"],["June"],["July"],["August"],["September"],["October"],["November"],["December"]]
    var now = new Date();
    for (var i=0; i < ETable.length; i++) {
      if (now < ETable[i].date) {
        var html = "For " + months[ETable[i].date.getMonth()] + " " + ETable[i].date.getDate() + ", " + ETable[i].date.getFullYear() + "<br><br>";
    	document.write(html);
        document.write(ETable[i].text); return; } }
    document.write("We don't have any programs set up yet.<br>Please visit this site later.");
    }
    The whole thing is here and the "Upcoming Concerts" replaces the "return" with line feeds.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.12045 seconds
  • Memory Usage 2,882KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (5)bbcode_code
  • (1)bbcode_html
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (3)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (3)postbit
  • (3)postbit_onlinestatus
  • (3)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • bbcode_parse_start
  • postbit_imicons
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates