I would like to be able to click on the Orange text to be able to call the orange.js file, then load the .js file into the myTableResult div. I would like the name of the .js file to be inside the onclick event. Can somebody help me with that?
<a onclick="getScript('Orange.js');"/>Orange</a>
<div id="myTableResult">
</div>
<script type="text/javascript" src="/myapp/htmlCode"></script>
3
Answers
if you are using jquery, you can use:
to get the script and execute whatever command you want in the after load section (call functions, .etc).
if you are using vanilla javascript, then you need to first add your script to document body and then call any function you want like this:
And to pass result to your element you need to do
jquery:
vanilla js:
document.getElementById("myTableResult").innerHTML="your_html_goes_here"
maybe you need to especify a little bit more your question, but here is an example of html and js injection.
First i prefer to use button instend of a just because the side effects of a tag element here is the code:
Then on my "orange.js" file i do something like this:
Hope this could help you :).
You want to append HTML to the DOM so given a file named
Orange.html
containing the following HTML:Orange.html
index.html
The following code will allow you to append the table to the DOM using fetch:
I have made the code extensible so for each
<a href="#" class="load-table-link">
you just need to add the name of the file containing your html tables in thedata-file
attribute and the targetdiv
id in thedata-destination
attribute.