Make Java script click event dynamic

Hi Experts,
I have this Java script. How can I make the click dynamic so that this function can be called on the click event of any button.

Thanks.

<script type="text/javascript">
        $(document).ready(function () {
            $('#btnSubmit').click(function () {
                $('.blockMe').block({
                    message: 'Please wait...<br /><img src="Image/Wait.gif" />',
                    css: { padding: '10px' }
                });
            });
        });
</script>
RadhaKrishnaKiJayaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeffrey Dake Senior Director of TechnologyCommented:
Jquery uses selectors just like css. You currently are using the #btnSubmit, which is looking for an element with an id of btnSubmit. If you change that to A it would then select all links on the page, so if an anchor tag was clicked the function would be executed.

$('A').click(function () {

Hope that is what you are looking for
Prakash SamariyaIT ProfessionalCommented:
Use jQuery button selector $( ":button" ) or $( "button, input[type='button']" ) like below
<script type="text/javascript">
        $(document).ready(function () {
            $(':button').click(function () {
                $('.blockMe').block({
                    message: 'Please wait...<br /><img src="Image/Wait.gif" />',
                    css: { padding: '10px' }
                });
            });
        });
</script> 

Open in new window

RadhaKrishnaKiJayaAuthor Commented:
Hi Prakash,

Thank you for your reply. How can I pass the ID of the button to this function. For example if submit button is click then it should btnSubmit and if btnSave is clicked then btnSave.

Thanks.
Prakash SamariyaIT ProfessionalCommented:
Event always pass with current object, just need to get that object. You can directly use "this" which represents the current object.  Or you can pass event object and using that event object you can get your things (like id). Please see modified code below for your reference:

Directly use "this" as current object
<script type="text/javascript">
        $(document).ready(function () {
            $(':button').click(function () {
				//Directly Use this object in jQuery
				if($(this).attr("id")=="btnSend"){
					//your code
				}
				//...other stuff
                $('.blockMe').block({
                    message: 'Please wait...<br /><img src="Image/Wait.gif" />',
                    css: { padding: '10px' }
                });
            });
        });
</script>

Open in new window


Fetch current object into variable and use them
<script type="text/javascript">
        $(document).ready(function () {
            $(':button').click(function (event) {
				//object of fired event will come to "event" object
				if(event.target.id=="btnSend"){
					//your code
				}
				//...other stuff
                $('.blockMe').block({
                    message: 'Please wait...<br /><img src="Image/Wait.gif" />',
                    css: { padding: '10px' }
                });
            });
        });
</script>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RadhaKrishnaKiJayaAuthor Commented:
Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.