Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Im using the following code:

jQuery(document).ready(function () {

jQuery('<%= btnSave.ClientID %>').click(function(){

    alert('hello world');

});

});

And when I click the asp.net button:

<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />

It just doesnt fire. Anybody know why?

Cheers, Pete

share|improve this question
add comment

3 Answers

up vote 0 down vote accepted
jQuery(document).ready(function () {

jQuery('#<%= btnSave.ClientID %>').click(function(){

    alert('hello world');
});

});

And why you put .ClientID?? in the asp you dont put any class to the button

Sorry for my english

share|improve this answer
add comment

Hi Sorry I figured it out it needs a pound sign before the selector!

 jQuery(document).ready(function () {

jQuery('#<%= btnSave.ClientID %>').click(function(){

    alert('hello world');

});

});

share|improve this answer
add comment

Another alternative here is to use a class so you can move your code externally, for example:

<asp:Button ID="btnSave" CssClass="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />

Then your code can look like this:

jQuery(function($) {
  $('.btnSave').click(function() {
    alert('hello world');
  });
});

Which can be in an external .js, so your users download it once, not as part of the page each time. Alternatively, a bit less efficient is to use the attribute ends-with selector, like this:

jQuery(function($) {
  $('input[id$=btnSave]').click(function() {
    alert('hello world');
  });
});

This will select all <input> elements that have an ID ending in btnSave.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.