1

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.

3 Answers 3

2

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 :)

Sign up to request clarification or add additional context in comments.

Comments

0

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
   });
});

5 Comments

I'm not sure how ASP.NET handles it, but have a look at your HTML. Depending on what type of button is created, you might have to change the code. Example here : jsfiddle.net/jomanlk/PUpnr
Hi JohnP: I just checked and generated html looks like this : <input type="submit" name="target" value="Animate Me" onclick="return false;" id="target" />
Ok, like user Yads mentions, you need to remove that onclick for this to work. And use .val() to get the value
@JohnP, I dont have onclick in button markup. I only have onclientClick
I'm talking about onclick="return false;" in the HTML markup that you posted. Not sure how ASP works, but you need to remove that. Unless there is an ASP specific way to handle it.
0

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.

6 Comments

actually, I am using JQUery QUnit and I need to perform a click of button from QUnit test and then see what is effect after click. I can't use onclientclick
@DotnetSparrow, if this is just for a test, you can remove the onclick attribute using jQuery.
I can't sue it because I need to test it against server side button click
@Dotnetsparrow, the effect after the click will be a call to doPostBack. So do you want to attach a handler or do you want to test what happens when you click the button?
In test I need to first create event handler and then test the effect
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.