Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display JavaScript variables in a HTML page without document.write

I am trying to display some JavaScript variable on my HTML page.

I was first using document.write() but it use to overwrite the current page when the function was called.

After searching around, the general consensus was that document.write() isn't liked very much. What are the other options?

I found a page suggesting using .innerHTML but that was written in 2005.

A jsFiddle illustrating my problem http://jsfiddle.net/xHk5g/

like image 592
kishan patel Avatar asked Mar 13 '12 17:03

kishan patel


People also ask

What can I use instead of document write in JavaScript?

the alternatives: Example: document. getElementById('output1'). innerHTML = 'Some text!';

Can I use JavaScript variable in HTML?

You cannot use js variables inside html. To add the content of the javascript variable to the html use innerHTML() or create any html tag, add the content of that variable to that created tag and append that tag to the body or any other existing tags in the html.

Can we use document write in JavaScript?

The write() method in HTML is used to write some content or JavaScript code in a Document. This method is mostly used for testing purpose. It is used to delete all the content from the HTML document and inserts the new content. It is also used to give the additional text to an output which is open by the document.


1 Answers

Element.innerHTML is pretty much the way to go. Here are a few ways to use it:

HTML

<div class="results"></div> 

JavaScript

// 'Modern' browsers (IE8+, use CSS-style selectors) document.querySelector('.results').innerHTML = 'Hello World!';  // Using the jQuery library $('.results').html('Hello World!'); 

If you just want to update a portion of a <div> I usually just add an empty element with a class like value or one I want to replace the contents of to the main <div>. e.g.

<div class="content">Hello <span class='value'></span></div> 

Then I'd use some code like this:

// 'Modern' browsers (IE8+, use CSS-style selectors) document.querySelector('.content .value').innerHTML = 'World!';  // Using the jQuery library $(".content .value").html("World!"); 

Then the HTML/DOM would now contain:

<div class="content">Hello <span class='value'>World!</span></div> 

Full example. Click run snippet to try it out.

// Plain Javascript Example  var $jsName = document.querySelector('.name');  var $jsValue = document.querySelector('.jsValue');    $jsName.addEventListener('input', function(event){    $jsValue.innerHTML = $jsName.value;  }, false);      // JQuery example  var $jqName = $('.name');  var $jqValue = $('.jqValue');    $jqName.on('input', function(event){    $jqValue.html($jqName.val());  });
html {    font-family: sans-serif;    font-size: 16px;  }    h1 {    margin: 1em 0 0.25em 0;  }    input[type=text] {    padding: 0.5em;  }    .jsValue, .jqValue {    color: red;  }
<!DOCTYPE html>  <html>  <head>  <script src="https://code.jquery.com/jquery-1.11.3.js"></script>    <meta charset="utf-8">    <meta name="viewport" content="width=device-width">    <title>Setting HTML content example</title>  </head>  <body>    <!-- This <input> field is where I'm getting the name from -->    <label>Enter your name: <input class="name" type="text" value="World"/></label>        <!-- Plain Javascript Example -->    <h1>Plain Javascript Example</h1>Hello <span class="jsValue">World</span>        <!-- jQuery Example -->    <h1>jQuery Example</h1>Hello <span class="jqValue">World</span>  </body>  </html>
like image 128
pseudosavant Avatar answered Sep 27 '22 22:09

pseudosavant