I am trying to call a user defined function in jQuery:
$(document).ready(function() { $('#btnSun').click(function() { myFunction(); }); $.fn.myFunction = function() { alert('hi'); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btnSun">Say hello!</button>
I tried the following as well:
$(document).ready(function() { $('#btnSun').click(function() { myFunction(); }); }); function myFunction() { alert('hi'); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btnSun">Say hello!</button>
It doesn't seem to work! Any idea where I am wrong?
You need to use the script tag. As almost everything we do when using jQuery reads or manipulates the document object model (DOM), we need to make sure that we start adding events etc. as soon as the DOM is ready. If you want an event to work on your page, you should call it inside the $(document).
In jQuery we can assign function in custom namespace. We can add or remove the function from namespace. This mechanism provide the functionality of storing the related function in a namespace.
JavaScript runs in the browser and so no you won't be able to call it remotely. You will need to have access to the same script on your "other page" and add the loadAccountInformation function as a click handler to the link in the same way you do on the accounts page.
Just try this. It always works.
$(document).ready(function() { $('#btnSun').click(function() { $.fn.myFunction(); }); $.fn.myFunction = function() { alert('hi'); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btnSun">Say hello!</button>
If you want to call a normal function via a jQuery event, you can do it like this:
$(document).ready(function() { $('#btnSun').click(myFunction); }); function myFunction() { alert('hi'); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="btnSun">Say hello!</button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With