Solved

jQuery: Reset input to default value when document ready

Posted on 2013-10-23
5
495 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now