I am trying to print the set element through innerHTML, but I am not getting the desired result. While I tried with document.write()
,it is printing but I want to use only innerHTML.
Here is my source code:
JavaScript
x
12
12
1
<div id="demo"> </div>
2
<script type="text/javascript">
3
var i, item;
4
var setObj1 = new Set();
5
for(i=0;i<5;i++)
6
setObj1.add(i);
7
8
for (item of setObj1.values())
9
//document.write(item+",");
10
document.getElementById('demo').innerHTML="The set value is: "+ item;
11
</script>
12
Output: 4
Desire output: 0 1 2 3 4
Please suggest me how to use innerHTML to print the output.I have tried console log and document.write(), they are working.
Advertisement
Answer
You can store all the value in a variable and then set that to the the element. Please note you can use textContent
or innerText
when the htmlString is only text.
JavaScript
1
12
12
1
<div id="demo"> </div>
2
<script type="text/javascript">
3
var i, item;
4
var setObj1 = new Set();
5
for(i=0;i<5;i++)
6
setObj1.add(i);
7
var val = ''
8
for (item of setObj1.values())
9
val+=item + ' ';
10
11
document.getElementById('demo').textContent = "The set values are: "+val;
12
</script>