Dynamically add onclick event on button

Hi, can I ask some help.,I have button and I remove the onclick dynamically like this


 $('#mybutton').removeAttr("onclick",'').unbind('click');

but how can I get back my button to onclick ?

Thank you indvance.

Hi jemz,

The preferred way is using .on() and [URL=“http://api.jquery.com/off/”].off()
What exactly is it that you are trying to do?

Thank you pullo for the quick reply.

I have button (search)that queries to the database,if there is result found the button search will be disabled,but if no records found still i could pressed or click the button search.


 <button type="button" id="search" onclick="getRec();" >Search


Thank you in advance.

No probs :slight_smile:

What does the getRec function look like?

Hi Pullo, I solved it now thank you :slight_smile:

Hi jemz,

That’s good to hear :slight_smile:
Out of curiosity, how did you solve the problem?

Hi pullo, I just tried it this way


 $('#search').on('click',getRec);

I am using firefox,I did not tried to other browsers yet.