Solved

jQuery: Reset input to default value when document ready

Posted on 2013-10-23
5
513 Views
Last Modified: 2013-10-24
Using jQuery, how can I reset an input to its default value?  in Firefox, this code makes all inputs blank when the page is refreshed.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

history.navigationMode = 'compatible';
$('*').unbind();
$(window).unload(function () {
 scroll(0, 0);
 $('*').unbind();
 $('input[type=text], select, textarea').val('');
});

$(document).ready(function() {

});

</script>
</head>
<body>
 <div>
  <input type="text" size="3" maxlength="2" value="1" />
  <input type="text" size="3" maxlength="2" value="2" />
  <input type="text" size="3" maxlength="2" value="3" />
 </div>
</body>
</html>

Open in new window

0
Comment
Question by:hankknight
  • 2
  • 2
5 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 333 total points
ID: 39595467
onunload is unreliable
Why would you want to clear the values before the page is refreshed?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 167 total points
ID: 39595555
test page : http://jsfiddle.net/vWd7U/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

history.navigationMode = 'compatible';
$('*').unbind();
$(window).unload(function () {
 scroll(0, 0);
 $('*').unbind();
});

$(document).ready(function() {
 $(':text,select,textarea').val('');
});

</script>
</head>
<body>
 <div>
  <input type="text" size="3" maxlength="2" value="1" />
  <input type="text" size="3" maxlength="2" value="2" />
  <input type="text" size="3" maxlength="2" value="3" />
 </div>
</body>
</html>

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 39595652
GaryC123, I am using onunload to demonstrate the problem.  It is one of the ways that form values can get changed.  

leakim971, your code makes the inputs always blank.  They should be reset to the values "1", "2" and "3" respectively every time you refresh the page.
0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 333 total points
ID: 39595670
Don't make sense
You're making a problem out of a situation that makes no sense.
If the page is refreshed what does it matter what is in the textboxes, the elements cease to exist.
FF has aggressive caching and will autofill inputs etc with previous values. To stop this you can use autocomplete="off".  If you don't want to do it manually you can use jquery to set the attribute at load time.
0
 
LVL 16

Author Comment

by:hankknight
ID: 39599190
Thank you both for the help.  With your help I figured it out:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Demo</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

history.navigationMode = 'compatible';
$('*').unbind();
$(window).unload(function () {
 scroll(0, 0);
 $('*').unbind();
 $('input[type=text], select, textarea').val('');
});

$(document).ready(function() {

 $('input[value]').each(function() {
  $(this).val($(this)[0].defaultValue);
 });

});

</script>
</head>
<body>
 <div>
  <input type="text" size="3" maxlength="2" value="1" />
  <input type="text" size="3" maxlength="2" value="2" />
  <input type="text" size="3" maxlength="2" value="3" />
 </div>
</body>
</html>

Open in new window

0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question