1

How can I display a JavaScript variable as an paragraph(<p>) in a website

Comments
  • 2
    /*! jQuery v3.3.1 | (c) JS Foundation and other contributors | jquery.org/license */

    !function(e,t){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=e.document?t(e,!0):function(e){if(!e.document)throw new Error("jQuery requires a window with a document")...

    $( 'body' ).append(`<p>${var}</p>`);
  • 0
    It depends on _when_ do you want to write that variable to the document.

    If you set the variable in the <head> and you want to write it right away in the document, just put this wherever you want to write the variable:

    <p><script>document.write(variable);</script></p>

    Or if you want to include the <p> tag in the operation:

    <script>document.write(`<p>${variable}</p>`);</script>

    If you want to write the paragraph when a certain event occurs and not as the page is loading, you can use jQuery as above or go for the longer, vanilla DOM way:

    var p = document.createElement('P');

    var t = document.createTextNode(variable);

    p.appendChild(t);

    document.querySelector('body').appendChild(p);
  • 2
  • 0
    @ethernetzero thank you very much
  • 0
    @nate thanks!
  • 0
    always remember injection, kids!
Add Comment