passing a javascript variable to html

I am facing an issue with two files, filter.html and filter.js. The file filter.html contains a div with the id "test", while the file filter.js has a variable named "finishedHTML."

My objective is to inject the content of "finishedHTML" into the test div as html code. However, "finishedHTML" seems to be empty. How can I resolve this problem?

Source:

var finishedHTML = "<h2>Yes it works</h2>"
header {
  display: block;
  background: #A2AFBC;
  top: 0;
  height: 50px;
  position: fixed;
  width: 100%;  
  text-align: center;
}

footer {
  height: 50px;
  width: 100%;
  bottom: 0px;
  left: 0px;
  background: #A2AFBC;
  position: fixed;
}
.headerTitle {
  font-size: 2.0em;
  font-family: Verdana;
  font-weight: 100;
  color: #506B84;
  margin: 0em;
  font-weight: bold;
}

h2 {
  font-size: 3.0 em;
  color: red;
}
<html>    
<head>
    <title>title</title>
    <link rel="stylesheet" href="style.css" type="text/css" >
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" >
    <link rel="shortcut icon" type="image/x-icon" href="img/favicon.ico" >
    <!--Filter.js-->
    <script language="javascript" type="text/javascript" src="filter.js"></script>
</head>
<body>
    <header>
        <span class="headerTitle">Header Text</span>
    </header>

    <div id="test" style="margin-left: 300px; padding-top: 300px;">
        <h1>This is a test text</h1>
    </div>
    <script language="javascript" type="text/javascript">
        document.getElementById('test').innerHTML = finishedHTML;
    </script>
    <footer>
    </footer>
</body>
</html>

Thank you for your assistance!

Answer №1

To ensure the variable finishedHTML functions properly, it must be declared before use. Simply declare it at any point prior to assigning the innerHTML, and it will execute as intended.

var finishedHTML = "<h2>Yes it works</h2>";
document.getElementById('test').innerHTML = finishedHTML;
header {
        display: block;
        background: #A2AFBC;
        top: 0;
        height: 50px;
        position: fixed;
        width: 100%;  
        text-align: center;
    }
    
footer {
        height: 50px;
        width: 100%;
        bottom: 0px;
        left: 0px;
        background: #A2AFBC;
        position: fixed;
    }
.headerTitle {
    font-size: 2.0em;
    font-family: Verdana;
    font-weight: 100;
    color: #506B84;
    margin: 0em;
    font-weight: bold;
    
}

h2 {
  font-size: 3.0 em;
  color: red;
  
}
<html>    
    <head>
        <title>title</title>
        <link rel="stylesheet" href="style.css" type="text/css" >
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" >
        <link rel="shortcut icon" type="image/x-icon" href="img/favicon.ico" >
        <!--Filter.js-->
        <script language="javascript" type="text/javascript" src="filter.js"></script>
    </head>
    <body>
        <header>
            <span class="headerTitle">Header Text</span>
        </header>
        <!-- style="margin-left: 300px; padding-top: 300px;"-->
        <div id="test" style="margin-top: 100px;">
            <h1>This is a test text</h1>
        </div>
        
        <footer>
        </footer>
    </body>
</html>

Answer №2

<html>    
    <head>
        <title>title</title>
        <link rel="stylesheet" href="style.css" type="text/css" >
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" >
        <link rel="shortcut icon" type="image/x-icon" href="img/favicon.ico" >
    </head>
    <body>
        <header>
            <span class="headerTitle">Header Text</span>
        </header>

        <div id="test" style="margin-left: 300px; padding-top: 300px;">
            <h1>This is a test text</h1>
        </div>
        <!--Filter.js-->
        <script language="javascript" type="text/javascript" src="filter.js"></script>
        <script language="javascript" type="text/javascript">
            document.getElementById('test').innerHTML = finishedHTML;
        </script>
        <footer>
        </footer>
    </body>
</html>

Placing <script> tags inside the body can help resolve any issues related to asynchronous script loading.

Answer №3

Just wanted to share that everything seems to be working perfectly for me. Thank you!

<html>    
    <head>
        <title>My Title</title>
        <link rel="stylesheet" href="style.css" type="text/css" >
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" >
        <link rel="shortcut icon" type="image/x-icon" href="img/favicon.ico" >
        <!--Filter.js-->
        <script language="javascript" type="text/javascript" src="filter.js"></script>
    </head>
    <body>
        <header>
            <span class="headerTitle">Header Text</span>
        </header>
        
        <div id="test" style="margin-left: 300px; padding-top: 30px;">
            <h1>This is a test text</h1>
        </div>
        <script language="javascript" type="text/javascript">
            var finishedHTML = "<h2>Yes it works</h2>";
            console.log(finishedHTML);
            document.getElementById('test').innerHTML = finishedHTML;
        </script>
        <footer>
        </footer>
    </body>
</html>

Answer №4

Reposition

document.getElementById('test').innerHTML = finishedHTML;
in the filter.js file.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is causing the col-auto with the red border to exceed its available height?

For this code, I've utilized version 5 of Bootstrap. In full screen width, the col-auto with the red border seems to be taking up more space than its content. The yellow border marks the available content space inside. I'm curious about what migh ...

Determining the pixel padding of an element that was initially set with a percentage value

I am working with a div element that has left padding assigned as a percentage, like so: padding-left: 1%; However, I need to obtain the value of this padding in pixels for some calculations after the window has been resized. When using JavaScript to chec ...

Difficulty in making Materialize.css column match the height of the entire row

Utilizing React.js, Materialize.css, and certain MaterialUI components in collaboration to build a website. Developed a header (React component, basic div with title and logout button) for the site using a Materialize.css "grid". Issue: The react compone ...

Press the html button and activate the button using Visual Basic

Being new to Visual Basic, I have created a simple app using the WebBrowser component. The web page that loads contains an HTML button. I want this HTML button press to enable a button in Visual Basic, but I don't know how to do it. I have researched ...

I possess a certain input and am seeking a new and distinct output

I am looking to insert a word into an <input> and see an altered output. For example, Input = Michael Output = From Michael Jordan function modifyOutput() { var inputWord = document.getElementById("inputField").value; var outputText = "print ...

What is the best way to make an image expand when clicked, align it in the center of the webpage, and have it return to its original position with just one more click by utilizing

Currently, this is the code I am working with. The JavaScript functionality is working well, however, there seems to be an issue where the image does not return to its original size on a second click. Additionally, I am having trouble getting the CSS to ce ...

Tips for expanding a flex-grow element to fill the area of a concealed flex item

I am looking to create a window that can be divided into two or three areas based on the state of a checkbox. When the box is checked, I want the second area to be hidden and the first area to expand to fill the additional space. My layout works perfectly ...

On the initial page load, Magento is failing to load the CSS properly

I've been tinkering with the Magento website over at For some reason, the CSS on the Home page doesn't load initially, but it loads fine when you click on any other link like Products or Gallery. Any ideas why this might be happening? Thanks in ...

The Ajax request is not being triggered when using a different form submit method

Being a tech enthusiast, I have developed a handy function: function blur_slide_visit_count(){ $.ajax({ type: 'POST', url: 'add_save_slide_visitor_count.php', async: false, data: { fillvalue: fieldAr ...

Transforming the input button into images

I'm new to JavaScript and I'm looking to change the show button and hide button to images instead. The show button image should be different from the hide button image. Can anyone guide me on how to achieve this? Click here for reference $( ...

Trouble accessing environment variables within a React application, specifically when using webpack's DefinePlugin in a Dockerfile

I can't figure out why console.log is printing undefined. The files Makefile, config.env, webpack.config.js, and package.json are all located in the root of my project. Makefile: docker-run: docker docker run -it --env-file config.env -p "80:80" ...

Developing a bespoke React component library - encountering an issue with 'react module not found' during Jest testing, as well as using testing-library

I am in the process of creating a bespoke react component library to be shared across various applications. To build this library, I am utilizing rollup and referencing resources such as this blog post along with others: https://dev.to/alexeagleson/how-to- ...

Using MUI ClickAwayListener to automatically close the modal upon clicking in React.Js

In order for the modal to work correctly, it should be visible when the 'More' button is clicked and should close when either the More button or any other part of the screen is clicked (excluding the modal itself). I've attempted different m ...

Adjust tool tip text on the fly

As a beginner, I created a test table and now I want to make the tool tip text change dynamically when I hover over the table with my mouse. Ideally, I would like the tool tip text to correspond to the content of the table cell. Currently, I only have stat ...

Identify repeated entries in an HTML table by comparing the values in the first two columns

One of my requirements is to check for duplicate data in an HTML table. The table consists of two columns - product name and batch. While the product name can be repeated, if the same batch repeats for the same product name, I need to display an alert with ...

Can a custom structural directive be utilized under certain circumstances within Angular?

Presently, I am working with a unique custom structural directive that looks like this: <div *someDirective>. This specific directive displays a div only when certain conditions are met. However, I am faced with the challenge of implementing condit ...

I'm currently facing an AttributeError issue, and I'm seeking solutions to resolve it

Currently, I am in the process of developing an eCommerce platform using Django. However, I have encountered an issue when attempting to manipulate the quantity of each product in the cart. The error message displayed is as follows: AttributeError at /orde ...

The data from my API is not showing up in my React application

Im working on a project where I am trying to retrieve an image of a recipe card from and display it on my react.js application. However, I am encountering difficulties in getting the API data to show up on the page when running the code. Any assistance wo ...

Issue with transition not functioning properly on a rotated CSS3 element

Having a bit of trouble with my CSS class: .rotate { -webkit-transition: all 2s ease-in-out; -webkit-transform : rotateY(360deg); } When I change the rotation to 180deg and apply the class using jQuery, the div rotates instantly instead of over a 2 s ...

Enhancing FileUpload functionality in ASP.NET with jQuery function prior to postback

My webpage is built with .aspx and contains the following code: .. <form id="frm" runat="server"> <asp:FileUpload runat="server" id="fileupload" onchange="browsed()" /> <asp:Button runat="server" OnClick="Upload_Click" id="uploadb ...