Modifying the content of a div element on the main webpage by utilizing JavaScript from a separate page

(primary.html)

<div>
    Insert Any Text Here
</div>

(secondary.html)

<button class="start" onclick="updateContent()">Start</button>

(script.js)

function updateContent()
{
  // How can I dynamically change the content in primary.html from here?  
}

Clicking the button should trigger a change in the content displayed on primary.html. I've attempted using localStorage, but it wasn't successful. Any assistance with this matter would be greatly appreciated!

Answer №1

(index.html)

<div id="content">
    Insert your text here
</div>

(script.js)

document.addEventListener('DOMContentLoaded', function(){
   const contentDiv = document.getElementById('content');
   const info = sessionStorage.getItem('info');
   if(info){
       contentDiv.innerHTML = info;
   }
});

(page2.html)

<button class="home" onclick="addInfo()">Home</button>

(page2.js)

function addInfo()
{
  sessionStorage.setItem('info', 'new value');
}

Answer №2

Client-side storage in LocalStorage allows values to be stored permanently, ensuring that when a button is clicked, the value will be changed forever. This change will be reflected even after reopening the browser. To update the value every time the button is clicked, simply replace "hello" with the desired text manually.

To avoid the need for manual code changes each time, consider incorporating an input field on page2.html to store the desired text in LocalStorage and display it on another page.

<div id="page1">
    Any Text Here
    
</div>
<script>
    document.getElementById("page1").innerHTML=localStorage.getItem("page2");
</script>
<button id="home" class="home" onclick="change();">Home</button>

<script>
function change()
{
  //How can I change the content of the div on main.html from here?
 x="hello";
 localStorage.setItem("page2", x);
}
</script>

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

Leverage the power of JavaScript to bring in a JSON file

Currently, I am working on building a website and utilizing a json file to store the necessary data for one of the pages. However, I am encountering difficulties when attempting to successfully import the data. My coding platform is repl.it. My attempts s ...

Struggling to choose an element by ID and classname?

Currently, I'm diving into the world of HTML and CSS and have encountered a question about selecting elements in CSS. I understand that we use .Classname{} to select a class and #IDname{} to select an ID. But my main confusion lies in trying to selec ...

iOS devices do not support the add/removeClass function

This code functions properly on desktop browsers, but encounters issues when used on iPhones. Furthermore, the script mentioned below seems to be causing problems specifically on iPhone devices. var $event = ($ua.match(/(iPod|iPhone|iPad)/i)) ? "touchstar ...

What are some strategies for preventing the $window.alert function from being triggered within an AngularJS controller's scope?

How can I prevent the alert from appearing on my dashboard? Do you have any suggestions? Thank you! I attempted to override the alert empty function in my controller, but I am still encountering the window alert when I navigate to my page. $window.alert ...

Deactivate the child division by the nth parent division

At the end of my question, I have included a dynamically created div structure with the id "forth-three-one". Now, I need to find a way to disable or hide the input inside this specific div. Directly using the id "forth-three-one" is not an option as it w ...

Creating a Dynamic Controller with Dynamic Parameters in AngularJS

For my project, I am using requirejs, angularamd, and ui.bootstrap. When working with a popup form, I utilize $uibModal from ui.bootstrap. However, I am facing an issue where I cannot pass a parameter "items" from resolve. How can I dynamically inject para ...

Having difficulty reaching a div element using either its id or class in a CSS stylesheet

I'm having trouble adding padding to the home section div and I can't seem to access it using either the id or class. Any suggestions on how to solve this issue would be greatly appreciated. Below is the CSS code: body { margin: 0 ; font ...

Stack bind error encountered

Has anyone encountered a javascript error that they can't seem to figure out? I'm getting the following error message: $("input[type=text],input[type=password],input[type=checkbox],input[type=radio],select,textarea").bindIntoStack is not a funct ...

The reference is set to "#report-item" without displaying it in the URL - the behind-the-scenes functionality explained

Can someone explain to me the functionality on this website? When I hover over the "report ad" button, the link appears as ....com/***/***#report-item. However, upon clicking it, a pop-up appears without changing the original URL to ....com/***/***#report- ...

Selenium alert: element click blocked: Another element is in the way of receiving the click

I'm currently using Selenium in an attempt to click on the "show more" option under the "about this show" section on the following URL: https://www.bandsintown.com/e/1024477910-hot-8-brass-band-at-the-howlin'-wolf?came_from=253&utm_medium=we ...

The video controls cannot be seen

I'm encountering an issue while trying to incorporate controls into a full-width HTML5 video. Despite following this example: https://jsfiddle.net/nfouvpe5/, the controls remain invisible. Below is the code snippet I am using: <div class="overlay" ...

creating a fresh window using AJAX in Ext JS

While I know there may be similar questions out there, the details of my situation are unique. I'm facing an issue with building a new window using ExtJS, starting from a pre-existing grid. The goal is to populate this new window with references to el ...

Cannot get form submit to function in Webkit Playwright

I am currently in the process of testing a basic login page using Playwright for automation. <form method="POST" name="login"> <input type="hidden" name="captcha_response"> <input type="hi ...

Leverage the leaflet search plugin alongside the $GetJson functionality

My request is to make the leaflet search plugin functional with an external .json file connected to the map. To elaborate on my goal, I will provide two code examples: The first one is sourced from an offline file with a geoJSON js file attached: var s ...

What is the best way to verify the type of an object received from request.body in Typescript

Is it possible to check the object type from the request body and then execute the appropriate function based on this type? I have attempted to do so in the following manner: export interface SomeBodyType { id: string, name: string, [etc....] } ...

Unsure of how to create a record of calculations made on the calculator

I am struggling with creating a calculator that includes a history feature. I have the basic functioning of the calculator working, but now I want to modify it so that it displays a history of operations performed by the user. The goal is for the history t ...

Trouble with HTML2PDF.js - download not being initiated

Currently, I am utilizing html2pdf.js in my project by following this tutorial: https://github.com/eKoopmans/html2pdf.js However, I've encountered an issue where despite implementing everything as instructed, the download prompt for the specified div ...

Retrieving data from AJAX requests

I have an AJAX code that sends data to the server and returns results including id, name, and quantity. How can I extract and print only the quantity, id, or name? Thank you for your help! <script type="text/javascript"> $("#bto_update_quan ...

Breaking down arrays using the JADE Template Engine

Currently, I am utilizing the JADE template engine in conjunction with ExpressJS. I am attempting to pass an array to my JADE template like so: var data = { "labels" : ["Label 1", "Label 2"] }; res.render('index', {data: data}); The struct ...

What causes a dot to display when hovering over a link in a list?

Good day. [Click here to access the test page](http://89.111.180.28/CatalogOfProductsAndServices.php) Code: <ul> <li> <a href="#">Бытовая техника</a> <ul> <li><a href="CatalogOfProductsAndServices.php ...