Click here to Skip to main content
15,915,703 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to change selectionIndex on button click in jquery
My Code is

ASP.NET
<asp:DropDownList runat="server" ID="d">

</asp:DropDownList>


What I have tried:

$(document).ready(function () {
try {

$('#<%=d.ClientID %>').on('change', function () {
try {
var ban = $('#<%=d.ClientID %>').val();
alert(ban);

}
catch (e) {

}
});
a();
}
catch (e) {
alert(e);
}

});


function a() {
try
{ $("[id*=d]").empty();
var _select = $("<select>");
_select.append($("<option value='0'>Select One</option>"));
_select.append($("<option value='1'>One</option>"));
_select.append($("<option value='2'>Two</option>"));
$("[id*=d]").append(_select.html());
$('#<%=d.ClientID %>').val('1');
$('#<%=d.ClientID %>').change();

}
catch(e)
{
alert(e);
}
}

It is not working.



If I add statically option to

<option value='0'>Select One</option>
<option value='1'>One</option>
<option value='2'>Two</option>
and modify a() function
function a() {
try
{
$('#<%=d.ClientID %>').val('1');
$('#<%=d.ClientID %>').change();

}
catch(e)
{
alert(e);
}
}
Now it is working
Posted
Updated 23-Mar-16 1:12am
v9
Comments
Vikram Dalwadiya 18-Mar-16 9:37am    
r u want selected value of drop-down on click button event ?
Member 7909353 21-Mar-16 2:56am    
no I want to change index and get selected value on click buton event

JavaScript
function a(Value)

{

                    _select.append($("<option value='0'>Select One</option>"));
                    _select.append($("<option value=" + Value+ ">" + NameText + "</option>"));
                    $("[id*=ddlStateMain]").append(_select.html());
                   
                 
                    $('#<%=ddlStateMain.ClientID %>').val(Value);
                    $('#<%=ddlStateMain.ClientID %>').change();
}
 
Share this answer
 
v2
Use The Below code It's will be working
$("select#elem").val('0');

instead of select#elem u can give Your Asp Dropdown Id
 
Share this answer
 
You've not explained what the problem is or what you're trying to do so I can only guess that what you actually mean is this;

var ban = $('#<%=d.ClientID %>').val();
 
Share this answer
 
Comments
Member 7909353 21-Mar-16 2:39am    
I want to change index on button click
F-ES Sitecore 21-Mar-16 5:28am    
$('#<%=d.ClientID %>').val('1');
Member 7909353 21-Mar-16 6:21am    
It is working but do not call indexchange event


$(document).ready(function () {
try {
$('#<%=d.ClientID%>').on('change', function () {
try {
var StateCode = $('#<%=d.ClientID %> :selected').val();

}
catch (e) {

}
});

}
catch (e) {
alert(e);
}

});
F-ES Sitecore 21-Mar-16 6:48am    
var ddl = $('#<%=d.ClientID %>');
ddl.val('1');
ddl.change();
Ram349 23-Mar-16 6:41am    
Use the the below code
$("select#elem").val('0');

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900