Guide: Sharing Data Between Pages Using onclick Event in JavaScript

1Page.js

function TableRow() {
        let cells = document.querySelectorAll('#recieve-info td');
        cells.forEach(cell => cell.onclick = function () {
            let prevcell = cell.previousElementSibling;
            if (prevcell) {
    
                let LSItems;
                let AddValue = '/images/back_arrow.png.png'
    
                if (localStorage.getItem('passvalue') === null) {
                    LSItems = [];
                } else {
                    LSItems = JSON.parse(localStorage.getItem('passvalue'));
                }
                
                LSItems.push([AddValue]);
                localStorage.setItem('passvalue', JSON.stringify(LSItems));
                let prev = prevcell.innerHTML;
                console.log(prev);
            }
        });
    }

I am hoping to transfer values with an onclick event. For example, clicking on 'Save1' should save the value only in that particular cell.

https://i.sstatic.net/qm270.png

2Page.js

function ParaG() {
  document.querySelector('.Second-Para').innerHTML = JSON.parse(localStorage.getItem('passvalue'));
}

In this section, I aim to retrieve the saved values.

https://i.sstatic.net/wtHwX.png

Answer №1

You have the option to utilize cookies.

To store a particular cookie value:

document.cookie = "option=Option1";

To retrieve the stored cookie value:

function fetchCookie(cookieName) {
  var key = cookieName + "=";
  var decodedCookieStr = decodeURIComponent(document.cookie);
  var cookiesArr = decodedCookieStr.split(';');
  for(var i = 0; i < cookiesArr.length; i++) {
    var currCookie = cookiesArr[i];
    while (currCookie.charAt(0) == ' ') {
      currCookie = currCookie.substring(1);
    }
    if (currCookie.indexOf(key) == 0) {
      return currCookie.substring(key.length, currCookie.length);
    }
  }
  return "";
}
console.log(fetchCookie("option"))

Additional information on managing cookies can be found at:

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

Using jQuery checkboxes with ajax for data submission and storage in PHP without the need for insertion

Seeking guidance on how to properly serialize elements value instead of pushing it, as I am encountering an issue where each value is being inserted into the database 9 times. Any assistance in figuring out this problem would be highly appreciated. The HT ...

Adding a trailing slash to the URL in an Express server causes it to be appended

I've encountered a strange issue with my express server. Whenever I try to use a specific URL route with the word 'bind,' an extra '/' is automatically added to it. This behavior isn't happening with other URLs that I've ...

I am encountering an issue where the useState hook is returning an undefined value on separate components, even after

When setting up a login context, I wrap all my routes with the context provider and pass the initial value using useState: <userContext.Provider value={{loggedUser, setLoggedUser}}> In LogInMenu.jsx, which is responsible for setting the loggedUser ( ...

What is the process for including a scope in an Angular.js HTTP controller?

I am looking to access a $scope variable within this controller in order to utilize Angular functions like ng-click and ng-change in my HTML. Currently, I am only able to use the $http function but unable to execute any Angular methods on it. I am struggli ...

Chrome Devtool reported an error, but the majority of them are in a null file

Currently grappling with an irksome problem in Vue.js. No matter how hard I try, I just can't seem to pinpoint the error. Even when setting a debugger, all it shows is an empty file. Please take a look at the image provided. Any assistance on identify ...

Javascript problem: Understanding the .children method and how to use it

I have implemented a basic JavaScript/CSS code to show a tooltip/enlarged photo feature. You can visit the actual page here. The thumbnail images are located on the right-hand side. The issue I am facing is that when using the mouseenter function, the to ...

What is the reason behind NumberFormat only recognizing four numbers?

Encountering an issue with inputting values into a TextField embedded in NumberFormat. Only three numbers are accepted before the field resets. I aim to divide the value in the TextField by a format based on the selection made in the drop-down menu. For ex ...

Searching for a method to execute actions prior to the "enter" event in ng-animate, similar to the beforeAddClass function

//trying to replicate the top animation with the enter-leave animation below app.animation('.answer-animation', function(){ return { beforeAddClass: function(element, className, done){ if (className == 'answer') { ...

Using Bootstrap to handle opening a link when a dropdown menu button is clicked

Utilizing Bootstrap in my main menu was essential for navigating through the numerous pages, subpages, and sub-subpages within my project. The dropdownmenu feature proved to be very helpful in this regard. However, my client now has a specific request - t ...

Success with inserting data into MySQL, however the database table remains empty

I've encountered an issue with my React/MySQL registration page. Despite running the insert query, the data is not being inserted into the database table. I'm uncertain about what could be causing this problem. Within the backend folder, my serv ...

Testing AngularJS components using mocking the document object

My current task involves testing an angular service that performs DOM manipulations using the $document service with jasmine. For example, it may append a directive to the <body> element. The service in question might look like this: (function(modu ...

What are the best techniques for implementing opacity in styled components within react-native?

Trying to achieve a lighter background color using opacity in react-native with styled-components. I'm looking to make the background appear faded by adjusting the opacity level, but nothing seems to happen when I apply my code. Any suggestions on h ...

Sending information from ngRoute resolve to the controller

I am currently working on an application that utilizes ngRoute to create a Single Page Application. One of the requirements is to ensure that the view is not loaded until the data has been successfully fetched from the database. While I have managed to ach ...

Synchronize information between mongoDB and firebase bidirectionally

My current situation: Having developed an application using React, NodeJS, and Electron, most of my users utilize the app offline. Next plans: With intentions to cater to mobile users as well, I am considering creating a mobile application using React-N ...

Is there a way to incorporate vue samples into an independent HTML document?

Striving to broaden my knowledge of Vue, I set out to create a page with tabs inspired by one of the Vue examples available at . However, an obvious error seems to be eluding me, as I encounter a syntax issue on the line import * as Tabs from 'vue-s ...

Encountering a JSLint error while attempting to import the 'aws-amplify' package in my main file

Recently, I installed the latest version of aws-amplify using npm. After running npm init with 'entryPoint.js' as the entrypoint file, I encountered an issue when pasting the following code at the top of entryPoint.js: import Amplify, {Auth} from ...

What is the method for extracting URI value in JavaScript?

Similar Question: How can I get query string values? grab query string using javascript I am working with a form URI where I need to extract a specific ATTRIBUTE value and then assign that value to a corresponding form field. For example, conside ...

The JDBC driver is giving inaccurate results when querying MySQL

Currently using a jdbc driver (node-jdbc) to obtain the schema with node js Data retrieved : https://i.sstatic.net/IQkc7.png After trying, I received the following result in the console () https://i.sstatic.net/Tdxze.png Why am I getting an incorrect ...

I am experiencing an issue with environment variables not appearing in my Context component on Next.js. Should I adjust the Next.js configuration or set up the Context to properly utilize the variables?

Why are Environment Variables working on every component inside /pages but not in my Context component in Next.js? Do I need to do some configuration in Next.js for this? (Note: The Shopcontext.tsx file is using a class component that I obtained from a tu ...

Retrieve information from the database without the need to refresh the page

I recently implemented an Ajax function to update information in my database, but I encountered a problem. The element displaying this updated information does not reflect the changes immediately; only after refreshing the page and reconnecting to the data ...