Junho Choi Junho Choi - 2 years ago 73
HTML Question

print js variable into innerHTML call in another js file

Basically I have two javascript files that are separated but linked to a single html file. In this html file, in the js sections (i.e script section) I have declared a variable which I would like to print in an innerHTML part of the other js file where I am currently making a table.

Here I declared the variable in the script section of the html file:

var varIwanttoprint = 'data/rnase.mtz'


Here is the code for the table

var tab1 = document.getElementById("tab1");
tab1.innerHTML ='<p><hr></p><h3>General data</h3>' +
'<table class="table-blue">'+
'<tr><td>Path</td><td>**The variable i want to print here**</td></tr>'


However, I can't seem to find a way to print the js value of the js variable inside the html table. Is there any way I can do this?

Thank you

Answer Source

If the innerHTML part of the code is in a function then you can pass the value as an argument.

If innerHTML not in a function make sure the js file which including the innerHTML code is bellow the script section of varIwanttoprint and then you can concatenate the value.

var tab1 = document.getElementById("tab1");
tab1.innerHTML ='<p><hr></p><h3>General data</h3>' +
'<table class="table-blue">'+
'<tr><td>Path</td><td>'+varIwanttoprint+'</td></tr>'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download