-2

I have a text box for email, for which there is a checkbox. If the checkbox is not checked, the email textbox is disabled. However, I notice that on Google Chrome when I press the back button, the email textbox stays no more disabled. In Firefox, however, there is no such problem. So, what can I do to solve this really annoying problem?

Is there a way, to check if the page was reached by back button, so that I can forcefully disable the textbox if the check box is not checked?

pythonic
  • 20,589
  • 43
  • 136
  • 219
  • Dupe: http://stackoverflow.com/questions/38666988/how-to-see-if-a-form-element-was-not-posted – Marc B Jul 29 '16 at 21:03
  • No, its not the same question. That problem was solved. This is another problem. Its about the enabling and disabling of textbox and not about reading from it. Basically, I am only concerned about the GUI here. – pythonic Jul 29 '16 at 21:04
  • Can you please share your code so we can give specific answer... There can be many bugs causing the problem... – Aayush Sinha Jul 29 '16 at 21:05
  • I think the code is fine, as I face no problems on firefox. My question is if there is a way to check if the page was reached by back button, so that I can forcefully take steps to tackle that problem. – pythonic Jul 29 '16 at 21:06
  • 1
    What about this? http://stackoverflow.com/questions/829046/how-do-i-detect-if-a-user-has-got-to-a-page-using-the-back-button does that answer your question about how to detect if someone pressed the back button? – Jeff Jul 29 '16 at 21:17

2 Answers2

0

You can explicitly disable your text box element using JavaScript:

if(document.getElementById('myCheckbox').checked === false){
    document.getElementById('myTextbox').disabled = true;    
}
Lloyd Banks
  • 35,740
  • 58
  • 156
  • 248
  • Yes I know, but that was not the question. The problem is that the textbox is no more disabled when the page is reached by pressing the back button. Maybe, I should try to catch the onload event and forcefully do this stuff over there. Or which event should I do this in? – pythonic Jul 29 '16 at 21:12
  • OK, I did that on document ready event ($(function()) and it solved the problem! – pythonic Jul 29 '16 at 21:17
-1

I solved this problem by forcefully doing this inside the document ready function, as shown below.

$(function()
{
    if (!document.getElementById('check_sendToEmail').checked)
    {
        document.getElementById('YourEMail').value = ''
        document.getElementById('YourEMail').disabled = true
    }
});
pythonic
  • 20,589
  • 43
  • 136
  • 219