Saturday 15 September 2012

javascript - jQuery .hide() is not working -



javascript - jQuery .hide() is not working -

i trying hide element based on 2 dates.

the expiration date , today's date. if today's date greater expiration date hide element else, show it.

this old code:

<script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> <script language="javascript> var expiredate = new date("10/21/2014"); //get today's date var todaysdate = new date(); //call sethours take time out of comparing if(expiredate.sethours(0,0,0,0) != todaysdate.sethours(0,0,0,0)); { //date equals today's date $( "#announcement" ).hide(); } </script>

on page element on have this:

<div id="announcement"> <h1>an announcement!</h1> <p>hello!</p> <br /> <hr /> </div>

this new code:

<script language="javascript"> $(document).ready(function(){ var expiredate = new date("10/23/2014"); //get today's date var todaysdate = new date(); //call sethours take time out of comparing if (expiredate.sethours(0, 0, 0, 0) > todaysdate.sethours(0, 0, 0, 0)) { //date equals today's date $("#announcement").hide(); } }); </script>

the new code works fine... needed allow page load first before hiding it.

try set code in document ready.

<script language="javascript"> $(document).ready(function(){ var expiredate = new date("10/21/2014"); //get today's date var todaysdate = new date(); //call sethours take time out of comparing if(expiredate.sethours(0,0,0,0) != todaysdate.sethours(0,0,0,0)); { //date equals today's date $( "#announcement" ).hide(); } }); </script>

notice missing double quote in

<script language="javascript>

javascript jquery hide

No comments:

Post a Comment