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.
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. |
|||
|
|||
3 comments :
Above code not work
Thanks.It helped me a lot & solved my problem.
above demo does not work.
Note: Only a member of this blog may post a comment.