So I have a DropDownList control generated by ASP.NET. I'm trying to attach a .change() function to it with jQuery. It fires, but only after the user clicks somewhere else, not at the moment the selected option changes. I need to have it fire when the
selected option changes, just like the ASP.NET OnSelectedIndexChanged event. Does anyone know why it is behaving this way. My friend who uses jQuery a lot, but not ASP.NET, thinks it's an issue with the control being generated by ASP.NET stopping the event
from firing correctly...
jquerychange event
"Do your damnedest in an ostentatious manner all the time."
There is nothing wrong with the control being generated by ASP.NET. Can you post the ASP.NET code containing the DropDown control.
I am unable to reproduce your issue, it's working on my side. Since its client dev are you able to show a sample online?
Thanks,
Janier Davila
MCTS .NET Framework 3.5, ASP.NET Applications.
"I would love to change the world, but they won’t give me the source code"
----------------------------------------------------
Please remember to click “Mark as Answer” on the post that helped you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
The event your looking for is the select event not the change event. select event occurs while a drop down still has focus where as the change event only fires after the dropdown loses focus.
The event your looking for is the select event not the change event. select event occurs while a drop down still has focus where as the change event only fires after the dropdown loses focus.
now that you posted the code i can see you need to refernce the clientID of the object not the id of the object. its a common problem with asp.net apps and javsacript.
$('<%=MyDropDownList.ClientID%>').select(function(e){alert('should work now');});
If you still having issues try this out to see if you can get anywhere:
<select onselect="alert('you selected');" onchange="alert('i changed');"><option>first</option><option>second</option></select>
this will show you the event cycle for a dropdown
Complex system fail 100% of the time due to an oversight of the most simplistic and basic concepts: reason.
"The select event is sent to an element when the
user makes a text selection inside it. This event is limited to <inpu"t
type="text"> fields and <textarea> boxes."
The change event is the appropriate to handle select tags (dropdown control) the way he wants to.
Janier Davila
MCTS .NET Framework 3.5, ASP.NET Applications.
"I would love to change the world, but they won’t give me the source code"
----------------------------------------------------
Please remember to click “Mark as Answer” on the post that helped you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
I stand corrected on the handler to use. though that changes little about the correct solution, he still needs to have the correct selector that we are pointing out.
Complex system fail 100% of the time due to an oversight of the most simplistic and basic concepts: reason.
Oh well, I digress 'cause I'm stubborn . I tried with the select event like you said and it did not work, just like the documentation
said. JQuery Select event only works with input and textarea tags because it's fired when the user select the text inside it.
In fact, he wanted to do something when the user changes the dropdown's value, which is why the change event is for.
Anyways, I agree with you on this:
itanex
though that changes little about the correct solution, he still needs to have the correct selector that we are pointing out.
Hopefully that fixed his issue.
Janier Davila
MCTS .NET Framework 3.5, ASP.NET Applications.
"I would love to change the world, but they won’t give me the source code"
----------------------------------------------------
Please remember to click “Mark as Answer” on the post that helped you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
ppalubinski
Member
49 Points
175 Posts
jQuery .change() event on dropdownlist isn't firing correctly
Mar 26, 2010 04:35 PM|LINK
$('#MyDropDownList').change(function() {
alert('It worked!');
});
So I have a DropDownList control generated by ASP.NET. I'm trying to attach a .change() function to it with jQuery. It fires, but only after the user clicks somewhere else, not at the moment the selected option changes. I need to have it fire when the selected option changes, just like the ASP.NET OnSelectedIndexChanged event. Does anyone know why it is behaving this way. My friend who uses jQuery a lot, but not ASP.NET, thinks it's an issue with the control being generated by ASP.NET stopping the event from firing correctly...
jquery change event
love13chess
Member
487 Points
103 Posts
Re: jQuery .change() event on dropdownlist isn't firing correctly
Mar 26, 2010 08:16 PM|LINK
Hi,
There is nothing wrong with the control being generated by ASP.NET. Can you post the ASP.NET code containing the DropDown control.
I am unable to reproduce your issue, it's working on my side. Since its client dev are you able to show a sample online?
Thanks,
MCTS .NET Framework 3.5, ASP.NET Applications.
"I would love to change the world, but they won’t give me the source code"
----------------------------------------------------
Please remember to click “Mark as Answer” on the post that helped you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
itanex
Participant
1074 Points
237 Posts
Re: jQuery .change() event on dropdownlist isn't firing correctly
Mar 26, 2010 08:40 PM|LINK
The event your looking for is the select event not the change event. select event occurs while a drop down still has focus where as the change event only fires after the dropdown loses focus.
try:
$('#MyDropDownList').select(function(e){alert('it worked!');});
Mark it answered if I answered your needs.
ppalubinski
Member
49 Points
175 Posts
Re: jQuery .change() event on dropdownlist isn't firing correctly
Mar 26, 2010 08:45 PM|LINK
Thanks for taking a look at the issue. Here is the ASP.NET code for the DropDownList:
<td>Payment Status:</td>
<td><asp:DropDownList ID="Update_StatusDropDownList" runat="server">
<asp:ListItem Value="Pay" Text="Pay" />
<asp:ListItem Value="TD" Text="TD" />
</asp:DropDownList></td>
I am developing the site on my local machine at the moment, so don't have a way of posting it on the web.
ppalubinski
Member
49 Points
175 Posts
Re: jQuery .change() event on dropdownlist isn't firing correctly
Mar 26, 2010 08:47 PM|LINK
Hey, I tried using the .select() event, and it didn't work...
MetalAsp.Net
All-Star
112718 Points
18367 Posts
Moderator
Re: jQuery .change() event on dropdownlist isn't firing correctly
Mar 26, 2010 08:52 PM|LINK
Look at the generated HTML. It may be caused by ASP.NET ID mangling (especially with usercontrols and master pages).
You can try this: $('#<%= MyDropDownList.ClientID %>').change(blahblah)
itanex
Participant
1074 Points
237 Posts
Re: jQuery .change() event on dropdownlist isn't firing correctly
Mar 26, 2010 08:53 PM|LINK
now that you posted the code i can see you need to refernce the clientID of the object not the id of the object. its a common problem with asp.net apps and javsacript.
$('<%=MyDropDownList.ClientID%>').select(function(e){alert('should work now');});
If you still having issues try this out to see if you can get anywhere:
<select onselect="alert('you selected');" onchange="alert('i changed');"><option>first</option><option>second</option></select>
this will show you the event cycle for a dropdown
Mark it answered if I answered your needs.
love13chess
Member
487 Points
103 Posts
Re: jQuery .change() event on dropdownlist isn't firing correctly
Mar 26, 2010 09:33 PM|LINK
Hi again
Like metalasp.net said you can do it like this
$(document).ready(function() { $('#<%= Update_StatusDropDownList.ClientID %>').change(function() { alert($(this).val()); }); });@itanex
The select event does not work like you said it does. Taken from here http://api.jquery.com/select/
"The
selectevent is sent to an element when the user makes a text selection inside it. This event is limited to<inpu"t type="text">fields and<textarea>boxes."The change event is the appropriate to handle select tags (dropdown control) the way he wants to.
MCTS .NET Framework 3.5, ASP.NET Applications.
"I would love to change the world, but they won’t give me the source code"
----------------------------------------------------
Please remember to click “Mark as Answer” on the post that helped you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
itanex
Participant
1074 Points
237 Posts
Re: jQuery .change() event on dropdownlist isn't firing correctly
Mar 26, 2010 09:45 PM|LINK
I stand corrected on the handler to use. though that changes little about the correct solution, he still needs to have the correct selector that we are pointing out.
Mark it answered if I answered your needs.
love13chess
Member
487 Points
103 Posts
Re: jQuery .change() event on dropdownlist isn't firing correctly
Mar 26, 2010 10:23 PM|LINK
Oh well, I digress 'cause I'm stubborn
. I tried with the select event like you said and it did not work, just like the documentation
said. JQuery Select event only works with input and textarea tags because it's fired when the user select the text inside it.
In fact, he wanted to do something when the user changes the dropdown's value, which is why the change event is for.
Anyways, I agree with you on this:
Hopefully that fixed his issue.
MCTS .NET Framework 3.5, ASP.NET Applications.
"I would love to change the world, but they won’t give me the source code"
----------------------------------------------------
Please remember to click “Mark as Answer” on the post that helped you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.