Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
156
rated 0 times [  157] [ 1]  / answers: 1 / hits: 30182  / 8 Years ago, fri, august 5, 2016, 12:00:00

I'm just a beginner, but can someone help me out?



I have an HTML page with a couple of input fields, and I want the fields to be cleared when the page is refreshed.



My HTML looks like this:



<input type='text' id='input_field' value=''>


Then, toward the beginning of my script, there's this:



document.getElementById('input_field').value = '';


This works fine in Firefox; when the page is refreshed, the value is emptied and the field is cleared.



It does not work in Microsoft Edge; when the page is refreshed, the actual value is emptied, but the contents of the field remain.



I've also tried wrapping my input element(s) in a form element, and using JavaScript to reset the form, but the results are the same.



As you can see, I've kept the value attribute in the HTML, but only for purposes of demonstration and experimentation. Omitting it has no effect in either browser. However, if I leave the HTML attribute but omit the JavaScript statement, both browsers behave differently: in Edge, the contents of the field catch up to the actual value (i.e., the field clears), but only after the page is refreshed twice; and in Firefox, this fails to update the value at all, no matter how many times the page is refreshed.



Finally, if I edit the source in any way and then reload the page in Edge, it empties the actual value and also clears the field, but only on that initial reload. After that, the problem persists. This makes me wonder if it's a cache issue.



Any help or suggestions?


More From » html

 Answers
16

First, thanks for the responses.



I had tried autocomplete='off' (sorry I forgot to mention that), but it didn't seem to work.



Just before checking here for replies, though, I decided to try one other experiment. I added a button to my page which, when clicked, would call a function containing the JavaScript .value = '' statement.



For whatever reason, that worked just fine; it emptied the value and cleared the field, which got me thinking that tying the reset to a specific HTML event might be the key. I tried a couple of things that didn't work, but finally I just replaced this:



document.getElementById('input_field').value = '';


. . . with this:



window.onload = function() {
document.getElementById('input_field').value = '';
}


. . . and that did it. Every time I refresh the page in either Firefox or Edge, the value is emptied and the field is cleared.



I'm not sure why that worked (like I said, I'm just getting started with this stuff), but there it is.


[#61125] Thursday, August 4, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alli

Total Points: 409
Total Questions: 101
Total Answers: 105

Location: The Bahamas
Member since Tue, Apr 27, 2021
3 Years ago
alli questions
Sat, Apr 23, 22, 00:00, 2 Years ago
Mon, May 18, 20, 00:00, 4 Years ago
Tue, Mar 24, 20, 00:00, 4 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
;