Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

How to Reset Dropdown List using jQuery

Mar 20, 2014
Introduction

Here I will explain how to reset dropdown
using jQuery or clear / reset dropdown selection to original or default value in jQuery.

Description:
  
In previous articles I explained
jQuery check if string contains special characters or not, jQuery check file size before upload, Redirect to another page after 5 seconds, Get Current Datetime, Auto refresh page , Disable right click on image and many articles relating to JQuery. Now I will explain how to reset dropdown using jQuery or clear / reset dropdown selection to original or default value in jQuery.

To clear or reset dropdown to initial value we have a different ways

First Method

Here we will find first element in the dropdown list and we will make that first element selected


$('#ddluser').find('option:first').attr('selected', 'selected');

Second Method

Here we will check dropdownlist value with “0” and make it selected. In case our dropdownlist starting value “1” then we need to use “val(1)


$('#ddluser').val(0);

Third Method

Here we will find first element in the dropdown list and we will make that first element selected


$('#ddluser option:eq(0)').attr('selected', 'selected');

If you want to complete example we need to write the code as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery reset dropdown values in jQuery</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(function() {
})
function resetdropdown() {
$('#ddluser').find('option:first').attr('selected', 'selected');
}
</script>
</head>
<body>
<form id="form1">
<div>
<b>Select User:</b>
<select id="ddluser">
<option value="0">Select user</option>
<option value="1">Suresh Dasari</option>
<option value="2">Rohini Dasari</option>
<option value="3">Praveen Alavala</option>
<option value="4">Sateesh A</option>
</select>
<input type="button" id="btnclear" onclick="resetdropdown()" value="Reset Dropdown" />
</div>
</form>
</body>
</html>

Demo



Select User:


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

3 comments :

Unknown said...

Above code not work

Anonymous said...

Thanks.It helped me a lot & solved my problem.

Anonymous said...

above demo does not work.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.