-1

After i load this :

$('#buy-div').load('../buyBar/buyBar.html',function() {
 //do some things here
 });

I would like to include this :

<script src="../buyBar/BuyBar.js"></script> //access some html that does not exist

Beacuse in this js i am looking for some html that does not exist only after the .load function is done. (such as getElementById or $('input').keyup(function() { that happens before the .load was finished.

Aksen P
  • 4,564
  • 3
  • 14
  • 27
phamng
  • 31
  • 1
  • 6
  • [This](https://stackoverflow.com/q/19737031/5463213) may help! – Shanteshwar Inde Jun 27 '19 at 07:15
  • Just because the elements don’t exist yet, does not mean you have to load the script itself only after they do … Wrap that part of the functionality into a _function_, that you can then call in the load method callback. – 04FS Jun 27 '19 at 07:29

2 Answers2

1

Just put the code that you want to run after the html is loaded in a function. Then call that function in the callback function of the .load('../buyBar/buyBar.html')

Assume "../buyBar/BuyBar.js" originally contains

document.getElementByID("#someElement").innerHTML = "...";

You can change it to

function someFunction(){document.getElementByID("#someElement").innerHTML = "...";}

Now just put <script src="../buyBar/BuyBar.js"></script> in the <head> as usual. Then do this:

$('#buy-div').load('../buyBar/buyBar.html',function() {
    someFunction();
    //do other stuff   
});
Ricky Mo
  • 6,285
  • 1
  • 14
  • 30
-1

I figure out i can do it by loading it when .load is done :

     let script = document.createElement('script');
      script.src = "../buyBar/Pay.js";
      document.body.append(script);  

This works but i am not sure is the best solution.

phamng
  • 31
  • 1
  • 6