Sebastian Delgado Sebastian Delgado - 5 months ago 11
Javascript Question

how to reload and keep the value of a textbox with pure javascript / jquery?

i'm working on a statistic chart that shows the number of sold items day by day during a month/year selected by the user, it works perfectly but i'm having trouble with the input that the user gives, i'm using a calendar that fills a text-box with the data submitted, so i can take it's value with jquery, the thing is that in order to do the query for the date submitted, i need to reload the page and keep the text-box with the value that user selected before.

My input (it also loads the calendar, i'm using a library for that)

<input type="text" id="monthInput"></input>


This is what i trigger when my text-box is filled with the input form the calendar

OnAfterChooseMonth: function( ){
var yearMonth = $("#monthInput").val();
console.log(yearMonth);

reloadMe();
}


function reloadMe(){
location.reload();
}


yearMonth is the variable that use to do the query of my data and populate my chart.

Thanks for reading.

Answer

You can try localStorage OR sessionStorage

if(localStorage.getItem("year_month")){
    var yearMonth = localStorage.getItem("year_month");
    // Do something with the saved yearMonth
}

OnAfterChooseMonth: function() {
    var yearMonth = $("#monthInput").val();
    console.log(yearMonth);
    localStorage.setItem("year_month",yearMonth)
    reloadMe();
}

function reloadMe() {
    location.reload();
}