In javascript suppose you have this piece of code:
<div>
<script type="text/javascript">var output = 'abcdefg';</script>
</div>
Is there any way to simply "echo" (using PHP terminology) the contents of output
into #test
? That is, to output a string inline, assuming you have no standard way of traversing or selecting from the DOM?
document.write("...");
does write the contents of output
, but in doing so, it replaces the entire document with the output
.
The solution I'm looking for should act the same way a PHP echo
would: write the output into the document inline.
You'd have to use document.write
[docs]:
<div id="test">
<script type="text/javascript">document.write('abcdefg');</script>
</div>
DEMO
With the caveat that it does not work in XHTML documents. See the documentation for more details.
In standards-based browsers:
document.getElementByID("test").textContent = output;
For broader support, you could use text in jQuery (or the equivalent method if your library of choice):
$('#test').text(output);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With