Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7634121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:05:18+00:00 2026-05-31T07:05:18+00:00

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

  • 0

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/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:05:20+00:00Added an answer on May 31, 2026 at 7:05 am

    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>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to display the box characters using PDCurses but for some reason
Alright... I'm trying to write some javascript code that will apply to an html
I am trying to get text to display using JavaScript but I can't seem
Im trying to display some files in a web page so the user can
I'm trying to display some large images with HTML img tags. At the moment
I am trying to display some message in span. but it is not working.
I'm trying to display some data from a list sharepoint and then using jquery
Right now, what i'm trying to do is display some lists on a page
I've been trying to use http://livepipe.net/control/tabs to get some javascript tabs up and running
I'm trying to create some javascript code that will display a javascript form (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.