How can I use js var in html? [duplicate] - javascript

This question already has answers here:
How to display JavaScript variables in a HTML page without document.write
(9 answers)
Closed 4 years ago.
I'm actually trying to use a js var in my html code like this :
In javascript code :
var test = 'This is a test';
And in the html :
<p>display var test here</p>
How can I display the value of my js var in html like this ?

HTML:
<p id="paragraph"></p>
JavaScript:
var test = 'This is a test';
document.getElementById('paragraph').innerHTML = test;

Related

Datatype .innerHTML [duplicate]

This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 2 years ago.
I´m getting a NaN out of this script, is it a data type problem and how do i solve it?
<script>
function calc(){
var profile = document.getElementById("profil");
var hours = document.getElementById("hour");
document.getElementById("result").innerHTML=profile*hours;
}
</script>
Present the result like this
<button onclick="calc()">Calc</button><br>
<p id="result"></p>
You have to get the value from the element.
var profile = document.getElementById("profil").value;
With .getElementById you get a DOM Element.
Supposing "profil" and "hour" are input elements, you want to obtain their values:
<script>
function calc(){
var profile = document.getElementById("profil").value;
var hours = document.getElementById("hour").value;
document.getElementById("result").innerHTML=profile*hours;
}
</script>

Clear data from getElementById innerHTML? [duplicate]

This question already has answers here:
How do I clear the content of a div using JavaScript? [closed]
(2 answers)
Closed 2 years ago.
document.getElementById('test').innerHTML = '<h1>Hello World</h1>';
Is there anyway that I can remove everything the code above put between two HTML tags?
I gather you want to remove the text between the <h1> and the </h1>. Simple:
const test = document.getElementById('test');
const child = test.firstChild;
child.innerHTML = "";

How do i view a Google chart in a JSP page? [duplicate]

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 6 years ago.
I am trying to convert my List of Doubles in EL to a Array in JavaScript for use further on with Google Charts.
But i am stuck here in the snippet it sees the var prices as an array of Characters.
<head>
${requestScope.prices} <!-- gives [130.98, 130.84, 133.23, 130.32] -->
<p id="demo"></p>
<script type='text/javascript'>
var prices = "${requestScope.prices}";
document.getElementById("demo").innerHTML = prices[2]; //Gives 3
</script>
</head>
You can try it using JSTL. Firstly, fill js array this like and access it.
var prices = [
<c:forEach var="price" items="${prices}">
<c:out value="${price}" />,
</c:forEach>
];
document.getElementById("demo").innerHTML = prices[2];

Attribute value as variable name [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 7 years ago.
I'm trying to get a dynamic attribute value to be used as a var name
What I have:
$(this).html(""+$(this).attr("id")+"");
It gives me the value as plain text. What I want is that it uses the attr("id") value as a Var like:
$(this).html(""+myVar+"")
Final example:
<div class="text" id="text1"></div>
<div class="text" id="text2"></div>
var text1 = "hello world"
var text2 = "bye world"
$(document).ready(function () {
$(".text").click(function(){
$(this).html(""+$(this).attr("id")+"");
});
});
Basically I want a dynamic way of calling to certain id's and getting their attribute value to be used as the Var name instead of plain text.
It all depends on variable namespace and scope, but generally next code should do the job.
$(".text").click(function(){
var varFromAttr = window[$(this).attr("id")];
$(this).html(varFromAttr);
});
Question is almost the same as this question.

Javascript equivalent of PHP's "print" [duplicate]

This question already has answers here:
Is there a php echo/print equivalent in javascript
(9 answers)
Closed 8 years ago.
If I have some PHP HTML that reads like this:
<html> I am feeling <?php print urldecode($_GET["emotion"]);?> today!</html>
and Get's "emotion" in URL title is "Happy", the HTML renders "I am feeling Happy today!".
Now, migrating away from PHP, how do I do this with javascript?
In Javascript I have a variable $emotion = "Happy"; so what Javascript goes inbetween the script tags below (where there are currently asterisks********)
<html> I am feeling <script>*********************</script> today!</html>
You can do it easily by putting an element where you want the text to change, then accessing that element by it's id.
$emotion = "Happy";
var e = document.getElementById('emotion');
e.innerHTML = $emotion;
I am feeling <span id="emotion"></span> today!

Categories

Resources