0

I made this fiddle http://jsfiddle.net/cyrtx7qw/1/ and I cant figure out how to make these two dates write on page load. I need them to check if the boxes are empty and then I want to write the today's date and today-7 respectively.

 if ($('#datePickersAppearance').is(':empty')) {
     var today = new Date();
     var curr_date = today.getDate();
     var curr_month = today.getMonth();
     curr_month++;
     var curr_year = today.getFullYear();
     document.getElementById("to").innerHTML = curr_date + "." + curr_month + "." + curr_year;

     today.setDate(today.getDate() - 7);

     var new_date = today.getDate();

     var new_month = today.getMonth();
     new_month++;
     var new_year = today.getFullYear();

     document.getElementById("from").innerHTML = new_date + "." + new_month + "." + new_year;
 }

Also, is there a way keep another date after page reload in case I change the default ones? I need the if statement to not trigger then...

3 Answers 3

4

filddle

if ($('#datePickersAppearance').is(':empty'))
 to
if ($('.datePickersAppearance').val() == '')



 document.getElementById("from").innerHTML

should be

document.getElementById("from").value
Sign up to request clarification or add additional context in comments.

6 Comments

Yes, that was it, so is there a way to get a newly typed date to stick after page reload? Because, right now, it will always revert to the default...
unlike the guy in that thread, I use MVC, so how would I go about it in that case then?
i don't know about MVC. but i dont think this is a difficult thing to maintain controls data across postbacks. @Vrankela
yeh sorry .. i noticed it now. reverted my answer. @Vrankela
|
1

The biggest problem is that <input> elements do not have a .innerHTML property. It is instead .value.


Your jQuery was traded for JavaScript.

Code was truncated for readability.

Use this instead:

(function loadDates(element){
    var from =  document.getElementById('from')
    var to =    document.getElementById('to')

    if(!from.value.length && !to.value.length ){

        var my_date = new Date()
        to.value =  my_date.getDate() + "." + my_date.getMonth() + "." + my_date.getFullYear()

        //  Redefine date object
        my_date.setDate(my_date.getDate() - 7)
        from.value =    my_date.getDate() + "." + my_date.getMonth() + "." + my_date.getFullYear()

    }//endif
})();

Comments

1

Updated, you can use sessionStorage to save your dates for page reloads. Also .blur() event handler is used to save data for sessionStorage, so that it's available when the page loads :-) Some other event handlers like .change() could be use as well. Fiddle

var today = new Date();
var curr_date = today.getDate();
var curr_month = today.getMonth();
curr_month++;
var curr_year = today.getFullYear();

var toDate = "";

//check if there's a to Date in sessionStorage 
if (sessionStorage.getItem("toDate")) {
  toDate = sessionStorage.getItem("toDate");
  sessionStorage.setItem("toDate", toDate);
} else {

  toDate = curr_date + "." + curr_month + "." + curr_year;
  sessionStorage.setItem("toDate", toDate);
}

document.getElementById("to").value = toDate;

today.setDate(today.getDate() - 7);

var new_date = today.getDate();

var new_month = today.getMonth();
new_month++;
var new_year = today.getFullYear();

var fromDate = "";

//check if there's a from Date in sessionStorage 
if (sessionStorage.getItem("fromDate")) {
  console.log("if");
  fromDate = sessionStorage.getItem("fromDate");
  sessionStorage.setItem("fromDate", fromDate);
} else {
  // actually else part of the code never runs because there's toDate in sessionStorage.
  console.log("else");

  fromDate = new_date + "." + new_month + "." + new_year;
  sessionStorage.setItem("fromDate", fromDate);

}

document.getElementById("from").value = fromDate;


$("#from").blur(function() {
  //check that there is data before saving into sessionStorage
  if (this.value.length >= 8)
    sessionStorage.setItem("fromDate", this.value)
});

$("#to").blur(function() {
  //check that there is data before saving into sessionStorage
  if (this.value.length >= 8)
    sessionStorage.setItem("toDate", this.value)
})

7 Comments

close, but something is wrong here, now I dont need the if ($('#datePickersAppearance').val('')) since you introduced the session storage. But even when I remove it, I still don't get the changed dates after refresh
The whole point of the check if empty was to see if a date was written before page reload
@Vrankela sorry I was away for a while :-) now I've added blur event handlers to save the session data so that it's available for page reloads. Also made the changes that you proposed i.e removed if ($('#datePickersAppearance').val('') check
I just tried it in my MVC visual studio application, it doesn't work. It wont display the changed dates after I reload the page, any thoughts?
@Vrankela are you using IE browser? If so, have you tried with some other browser like Chrome, Opera or etc. Alternatively, if you plan to use IE, then can you deploy it to IIS first and then try again. It could be that you need to access the page from Web server if sessionStorage is used with IE. More info: stackoverflow.com/questions/16212347/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.