jQuery Call (Custom) Function Examples
Did you want to call function in jQuery but did not know where and how to start? In this article, I am going to share a very easy way to use jQuery to call function / call custom function.
How to use jQuery to Call Predefine Function Or Custom JavaScript Function
In order to be able to call custom function or pre-defined function using jQuery, lets look at an easy example. We have already defined a custom function in our JavaScript code. Now when we click a button, we would like this custom function to be executed so that it gives us the corresponding output. So let’s see how this can be done:
Example: Call Custom Function using jQuery
<html>
<head>
<title>Call Custom Function using jQuery</title>
<script type="text/javascript" src="js/jquery_1.7.1_min.js"></script>
</head>
<body>
<div id="my_div" class="my_class">This is some content.</div>
<input type="submit" name="call_function" id="call_function" value="Call Function" />
<script type="text/javascript">
function my_function()
{
alert('This is my custom function call!');
}
$(document).ready(function() {
$('#call_function').click(my_function);
});
</script>
</body>
</html>Once you run the above code, simply click on the “Call Function” button to show the alert. This alert is inside our custom function. Can you beat that?
Your Turn!
Do you know of any other ways to define and call custom function using jQuery? Feel free to suggest by commenting below.