Search code examples
javascriptonchangeinput-field

javascript clearing an input onclick, but returning original value offclick


I have an input field that has today's date appearing onload. I also have a checkbox that clears the input field onclick. However, I'm hoping to have the original date field return if a user unchecks that box. Is this possible?

Checkbox HTML:

<span class = "required">*</span>$[SP]$[SP]<input type="checkbox" id="e_not_enroll" name="e_not_enroll"/>$[SP]$[SP] I do NOT want to enroll.

Date input box code:

<input type="text" id="d_date_of_event" value = "${jvar_employment_start_date}" style="background-color:#ddd" readonly="readonly"/>

if (document.getElementById("e_not_enroll").checked){           
    document.getElementById('d_date_of_event').value='';    
} else {
    document.getElementById('d_date_of_event').value = ?? ;
}

Solution

  • If you know the date before the JavaScript you can set a variable outside of the function to the original date. When the checkbox is unlocked simply put the original variable set outside back into the input field. The other alternative is to have another input field which is hidden. Called temp date or original day and use that to populate the original input on untick.