Home » Design, jQuery

Call jQuery function from javascript

2 August 2012 No Comment
Share on Facebook

JavaScript is a scripting language. jQuery is a framework that uses the JavaScript language to write JavaScript code faster. jQuery is a library written in JavaScript.

In most of the situation we might have wanted to call jQuery Function from javascript. That might not work as normal as how we call a javascript function from another js function or jquery function to another.

Tutorial difficulty:

Basic users

Problem: How to call jQuery function from javascript?

Solution: Create function declaration in script and function definition inside jQuery, so we have the handle of jQuery function outside to be called by javascript.

Let us see the solution with simple example.

 

 

Full Code to invoke jQuery function from js

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Invoke jQuery from javascript</title>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">

var jqueryFunction;

$().ready(function(){

//jQuery function
jqueryFunction = function( _msg )
{
alert( _msg );
}

})

//javascript function
function jsFunction()
{
//Invoke jQuery Function
jqueryFunction("Call from js to jQuery");
}
</script>
</head>
<body>
<a href="javascript:jsFunction();">invokeJSFunction</a>
</body>
</html>

Explanation of jQuery to js:

We are declaring the function initially.

var jqueryFunction;

And writing the function definition inside jQuery DOM ready function.

$().ready(function(){

jqueryFunction = function( _msg )
{
alert( _msg );
}

})

Here the “jqueryFunction” variables holds the handle of the jQuery function outside jQuery, hence we can call it from plain javascript.

function jsFunction()
{
jqueryFunction("Call from js to jQuery");
}

Conclusion:

we have seen how to invoke a jQuery function from js, please let us know your doubts or suggestion.

GET THE UPDATES VIA EMAIL

We don't share your email anywhere, grab our rss via feedburner