Domanda

I am creating a survey and I need the submit button to be hidden on a specific date. In other words, I need the button to be hidden on 10/22/2013 only and for the button to be visible all other days. I have been ripping my hair out figuring out why the code below does not work...am I missing something?...

var x=new Date();
x.setFullYear(2013,9,22);
var today = new Date();

if (x=today)
  {
  document.getElementById('NextButton').style.visibility='hidden';  
  }
else if
  {
  document.getElementById('NextButton').style.visibility='visible';
  }
È stato utile?

Soluzione

You are assigning instead of validating:

var nextBtn = document.getElementById('NextButton'),
    x = new Date(),
    today = new Date();

x.setFullYear(2013,9,22);

if (x === today) {
    nextBtn.style.visibility = 'hidden';  
} else if {
    nextBtn.style.visibility = 'visible';
}

Single = assigns, whereas == or === compares equality.

Side note:

=== is preferred (and therefore used above) because it verifies value and type. == verifies only value, i.e. 1 == '1' because the values match despite one being an integer and one being a string, however 1 !== '1' because while the value matches, the type does not. Just a little extra info.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top