Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jQuery: Reset input to default value when document ready

Posted on 2013-10-23
5
Medium Priority
?
556 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 1332 total points
ID: 39595467
onunload is unreliable
Why would you want to clear the values before the page is refreshed?
0
 
LVL 83

Accepted Solution

by:
leakim971 earned 668 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 1332 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…

885 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