I'd change the code a bit. FireClickMethod doesn't actually fire the click. It sets up jQuery to handle the click, but HandleClick isn't the right name either. Rather than a function to setup up the binding, I just put the code inside the .ready() function.
<script type=”text/javascript” >
$(document).ready(function () {
$(‘#btnTest’).click( function () {
Alert(‘ Button clicked’ );
})
});
</script>
- The ready function says, "When the page is completely loaded, run this code"
- The function in $("#btnTest).click() is an anonymous method (you didn't give it a name). It geta bound to the button's click event
- When you click the button, the anonymous method bound to it gets called.
- The function in .ready() is also an anonymous function
Now to address your question about your code:
- FireClickMethod() only gets called when .ready() runs (once per page load).
- The click method runs the anonymous function defined in $(‘#btnTest’).click()
- The anonymous method in the click is only bound to the btnTest button, not any of the other buttons.
>I want to be sure that I understand one basic concept of event handing by Javascript or jQuery correctly.
>
>A very simple example:
>
>Page has a few buttons and a few input elements. One of the buttons ID is ‘btnTest’
>
>Here is the script in the Head section of the page:
>
><script type=”text/javascript” >
> $(document).ready(function () {
> FireClickMethod();
> });
> Function FireClickMethod() {
> $(‘#btnTest’).click( function () {
> Alert(‘ Button clicked’ )
> })
> }
></script>
>
>
>Does method FireClickMethod fire when user clicks on any of the buttons? Of course I understand that jQuery will look for element ‘btnTest’ and will determine if the click was on this button. But I just wanted to understand that method files FireClickMethod() on every click or other event on the page. Do I understand this correctly?
Craig Berntson
MCSD, Microsoft .Net MVP, Grape City Community Influencer