Solved

jQuery: Reset all selects but not inputs

Posted on 2013-10-28
4
350 Views
Last Modified: 2013-10-28
Using jQuery, how can I reset all <select> values but not all <input> values?

In this example, all <select> values should be set to "xyz" when the button is clicked:
<!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">

$(document).ready(function() {

 $('input').val('123');
 $('select').val('0');

 $('#restAllSelect').click(function() {
  $('select').each(function() {
   $(this).val($(this).find('option:eq(2)').val());
  });
 });

});

</script>
</head>
<body>
 <div>
  <input type="text" value="abc" />
  <select>
   <option value="0">0</option>
   <option value="XYZ" selected="selected">XYZ</option>
   <option value="A">A</option>
   <option value="B">B</option>
   <option value="D">D</option>
   <option value="E">E</option>
  </select>
  <select>
   <option value="0">0</option>
   <option value="abc">abc</option>
   <option value="bbb">bbb</option>
   <option value="XYZ" selected="selected">XYZ</option>
  </select>
  <select>
   <option value="0">0</option>
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
   <option value="4">4</option>
   <option value="XYZ" selected="selected">XYZ</option>
  </select>
  <button id="restAllSelect">Reset selects but not inputs</button>
 </div>
</body>
</html>

Open in new window

0
Comment
Question by:hankknight
  • 2
4 Comments
 
LVL 44

Expert Comment

by:Rainer Jeschor
Comment Utility
Hi,
to set all selects to the value "XYZ" just use this:
 $('#restAllSelect').click(function() {
  $('select').each(function() {
   $(this).val('XYZ');
  });
 });

Open in new window

See:
http://jsfiddle.net/EE_RainerJ/3avYq/

HTH
Rainer
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
test page : http://jsfiddle.net/FjYYy/

$(document).ready(function() {

 $('input').val('123');
 $('select').val('0');

 $('#restAllSelect').click(function() {
    $('select option[value="XYZ"]').prop('selected', true);
 });

});

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
Comment Utility
Thank you both.  I should have specified that the point of this question is to RESET the value.  The actual values will not be XYZ.  They could be anything.  I want to reset the select elements to the value where the option has the attribute selected="selected".
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
check this : http://jsfiddle.net/FjYYy/4/
$(document).ready(function() {

 $('input').val('123');
 $('select').val('0');

 $('#restAllSelect').click(function() {
     $('select option').each(function() {
         if($(this).prop('defaultSelected')==true) $(this).prop('selected',true);
     });
 });

});

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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 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…

763 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

7 Experts available now in Live!

Get 1:1 Help Now