Reputation: 27996
I have an asp.net button on a page without masterpage like this:
<asp:Button ID="target" runat="server" Text="Animate Me" >
</asp:Button>
and I am calling this:
var panel = $('#<%= target.ClientID %>');
panel.click(function(){
alert($(this).attr("value"));
});
but no alert is shown. I even tried this:
$('#target').click(function(){
alert($(this).attr("value"));
});
but It didnt work.
Please suggest me how to click the button and what is issue in above code.
Thanks.
Upvotes: 1
Views: 19391
Reputation: 27996
Following code worked for me
var panel = $('#<%= target.ClientID %>');
eval(panel.trigger('click'));
and asp.net button looks like this.
<asp:Button ID="target" runat="server" Text="Animate Me"
OnClientClick="alert('button clicked');return false;" > </asp:Button>
hope it will help someone sometime :)
Upvotes: 2
Reputation: 17957
The reason is because an asp server button creates an inline onclick
handler that calls the doPostBack
function. This function is run and submits the form to the server. This happens before your handler is run. I would suggest using an html input button (non server), alternatively you can use the OnClientClick
attribute if you also need server side click handling.
Upvotes: 0
Reputation: 50019
Did you put your code inside $(document).ready()
Make sure it's wrapped
An example would be
$(document).ready(function(){
$('#target').click(function(){
alert($(this).val()); //use .val() if you're getting the value
});
});
Upvotes: 0