Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jQuery: Reset input to default value when document ready

Posted on 2013-10-23
5
Medium Priority
?
540 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 82

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

715 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