1

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

2
  • if the innerHTML part of the code is in a function, then you can call the function and pass the variable as an argument. Commented Jul 27, 2017 at 9:51
  • What syntax would I need to put the variable into the '<tr><td> XXXX </tr></td> part of the innerHTML? Commented Jul 27, 2017 at 10:00

2 Answers 2

1

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>'
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much. Because innerHTML was within another function, passing the variable I want to print as a parameter managed to fix it
Also, declaring it as a global variable seems to work as well
Happy to know that it helped :)
1

1st way

function retrunParam(){
var varIwanttoprint = 'data/rnase.mtz';return varIwanttoprint;

}


var tab1 = document.getElementById("tab1");
tab1.innerHTML ='<p><hr></p><h3>General data</h3>' +
'<table class="table-blue">'+
'<tr><td>Path</td><td>'+retrunParam()+'</td></tr>'

2nd way

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

 var varIwanttoprint = 'data/rnase.mtz'
document.getElementById("replaceHere").innerHTML = varIwanttoprint;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.