JavaScript function call inside jQuery

I use javascript and jQuery. My main file has My.js and Ajax.

My.js

 function build_one(){ alert("inside build_one"); } 

My main file

 <script type="text/javascript"> .. // Here I want to make call function defined in My.js build_one() .. // Here is the Ajax call $.ajax({ type:'POST', url: 'ajax.php', data:'id='+id , success: function(data){ $("#response").html(data); } }); ... </script> 

How do I make a call to the build_one () function before the Ajax function?

+4
source share
3 answers

This should work:

 <script type="text/javascript" src="My.js"></script> <script type="text/javascript"> build_one(); $.ajax({ type:'POST', url: 'ajax.php', data:'id='+id , success: function(data){ $("#response").html(data); } }); </script> 
+9
source

First you need to import the file before calling the function using the following

 <script type="text/javascript" src="My.js"></script> 

Now you can call your function wherever you want.

+5
source

I understood my question. :) The function defined in another file must be called outside of jQuery and assigned to a variable if you want to use the results inside jQuery. Hope a tidbit helps someone.

+2
source

All Articles