-1

I have a javascript file which I want to start when a div has been clicked.

I have already this when clicking the div "option_one", an input will be set to "Option 1":

$("#option_one").click(function() {
$("#input").val('Option 1');    
});

How can I fire another js file after clicking the div?

I try something with getScript but I couldn't get it working.

Kind regards,

Arie

Arie
  • 363
  • 4
  • 14
  • 1
    what do you mean with fire another js file? – Osama Jetawe Apr 23 '15 at 13:41
  • I don't understand you want to load another js file on ´#option_one´ click? – andybeli Apr 23 '15 at 13:45
  • just an javascript file, like calculating.js – Arie Apr 23 '15 at 13:45
  • 1
    if you include the file in the page on load, you will have access to the functions in that file so can just add them to your existing click event – Tomuke Apr 23 '15 at 13:46
  • @andybeli Yep, after clicking on the div option_one, I want to load the js file calculating.js in the .click(function) – Arie Apr 23 '15 at 13:48
  • possible duplicate of [Dynamically load JS inside JS](http://stackoverflow.com/questions/14521108/dynamically-load-js-inside-js) – fabian Apr 23 '15 at 13:55
  • Tnx for clearing this one out, I was making it to difficult ......... ;-) I wanted to load the file after clicking but loading the file at page load and calling the function was the solution for me! – Arie Apr 23 '15 at 13:55

2 Answers2

2

There is no need to load the calculate.js file on the click event. Just load it with the page load like so:

HTML:

<!--Your page HTML here... -->

<!--Before your closing body tag-->
<script type="text/javascript" src="somefile.js"></script>
<script type="text/javascript" src="calculate.js"></script>

JS:

// This is inside somefile.js
$("#option_one").click(function() {

    $("#input").val('Option 1');

    // This function is inside calculate.js
    calculate();    

});
Tomuke
  • 869
  • 2
  • 8
  • 26
0
$("#option_one").click(function() {
    $("#input").val('Option 1');

    $.getScript( "path/calculating.js" )
    .done(function( script, textStatus ) {
        //Just after the scripts finishes loading you will be able
        //to perform/use functionality included on this script
        console.log( textStatus );
    })
    .fail(function( jqxhr, settings, exception ) {
        console.log("Triggered ajaxError handler.");
    });
});
andybeli
  • 836
  • 7
  • 14