-3

I have a page with several divs, containing image with link, which I want to become from unclickable to clickable based on the date. For example - I have 5 divs for 5 days (01.03.2016, 02.03.2016, 03.03.2016, 04.03.2016, 05.03.2016). Inside these divs I have image with link.

on 01.03.2016 - only div 1 to be clickable, all others not

on 02.03.2016 - only div1 and div2 to be clickable, all other not and etc...

on 05.03.2016 - all five divs to be clickable

Thank you in advance

ataman79
  • 21
  • 1
  • 9

3 Answers3

0

I would use date comparison. Perhaps something like that:

HTML

<div class="date">01.03.2016</div>
<div class="date">02.03.2016</div>
<div class="date">03.03.2016</div>
<div class="date">04.03.2016</div>
<div class="date">05.03.2016</div>

JavaScript:

var elms = document.getElementsByClassName("date");
for (var i = 0 ; i < elms.length ; i++)
{
   var el = elms[i];
   var dmy = el.innerHTML.split('.');
   var date = new Date(dmy[2], dmy[1] - 1, dmy[0]);
   if (date <= new Date()) {
      el.style.backgroundColor = "yellow";
      el.onclick = function () {
         alert("you clicked on " + this.innerHTML)        
      };
   }
}

DEMO Here's a JSFiddle

guysigner
  • 2,822
  • 1
  • 19
  • 23
0

var divs = document.getElementsByClassName("date");

for (i = 0 ; i < divs.length ; i++) {
  divs[i].style.pointerEvents = 'none';
}

for (i = 0 ; i < divs.length ; i++) {  
  var divDate = divs[i].id.split('.');
  var date = new Date(divDate[2], divDate[1] - 1, divDate[0]);
  
  if (date <= new Date()) {
    divs[i].style.pointerEvents = 'auto';
  }
}
<div class="date" id="01.03.2016">01.03.2016 <a href="ihttp://google.com"><img src="images/image1.jpg" alt="Google Image" width="138" height="138" class="img_rounded" /></a></div>
<div class="date" id="02.03.2016">02.03.2016 <a href="ihttp://google.com"><img src="images/image1.jpg" alt="Google Image" width="138" height="138" class="img_rounded" /></a></div>
<div class="date" id="03.03.2016">03.03.2016 <a href="ihttp://google.com"><img src="images/image1.jpg" alt="Google Image" width="138" height="138" class="img_rounded" /></a></div>
<div class="date" id="04.03.2016">04.03.2016 <a href="ihttp://google.com"><img src="images/image1.jpg" alt="Google Image" width="138" height="138" class="img_rounded" /></a></div>
<div class="date" id="09.03.2016">09.03.2016 <a href="ihttp://google.com"><img src="images/image1.jpg" alt="Google Image" width="138" height="138" class="img_rounded" /></a></div>
Munawir
  • 3,346
  • 9
  • 33
  • 51
  • Hi @Munawir your code is working here when I run code snippet, but when I copy and paste into my local machine, it doesn't work? Do you know why ? – ataman79 Mar 08 '16 at 08:50
0

HTML

 <div class="date" id="First">01.03.2016</div>
 <div class="date all" id="Second">02.03.2016</div>
 <div class="date all three two"   id="Third">03.03.2016</div>
 <div class="date all three two one"   id="Fourth">04.03.2016</div>
 <div class="date all three two one zero"  id="Fifth">05.03.2016</div>

In JQuery Disable the click

  $("#First").click(function(){
     $(".all ").off('click');
   }
  $("#Second").click(function(){
     $(".three ")off('click');
   }
  $("#Third").click(function(){
     $(".two")off('click');
   }
  $("#Fourth").click(function(){
     $(".one").off('click');
   }
  $("#Fifth").click(function(){
     $(".zero")off('click');
   }

OR In JQuery Disable the div

  $("#First").click(function(){
     $(".date").removeAttr('disabled','disabled');
     $(".all ").attr('disabled','disabled');
   }
  $("#Second").click(function(){
     $(".date").removeAttr('disabled','disabled');
     $(".three ").attr('disabled','disabled');
   }
  $("#Third").click(function(){
     $(".date").removeAttr('disabled','disabled');
     $(".two").attr('disabled','disabled');
   }
  $("#Fourth").click(function(){
     $(".date").removeAttr('disabled','disabled');
     $(".one").attr('disabled','disabled');
   }
  $("#Fifth").click(function(){
     $(".date").removeAttr('disabled','disabled');
     $(".zero").attr('disabled','disabled');
   }
DAre G
  • 177
  • 10