Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a JavaScript function inside jQuery

I am using 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 the build_one() function call before the Ajax function?

like image 632
venkatachalam Avatar asked Dec 05 '22 07:12

venkatachalam


2 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>
like image 136
d4nt Avatar answered Dec 24 '22 15:12

d4nt


First you have to import your file before calling the function using the following

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

Now you can call your function where ever you want.

like image 26
Gumbo Avatar answered Dec 24 '22 15:12

Gumbo