Issue encountered while attempting to save a value in localStorage

I am encountering an issue while trying to save and read the value of a button in the panel. The error message I receive is - Unable to set property 'adl' of undefined or null reference. This is my first time working with localStorage, so I'm unsure where the problem lies. Can someone assist me in identifying the error?

Thank you.

Sincerely, Joe

Below is the code snippet:

<!DOCTYPE html> 
<html> 
<head> 
<title></title> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-2.1.3.min.js" data-semver="2.1.3" data-require="jquery"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script>
$(document).ready(function() {


    // Save settings

    $( "#myPanel" ).panel({
      beforeopen: function( event, ui ) {}
    });

    $( "#myPanel" ).on( "panelbeforeopen", function( event, ui ) {} );


    $( "#myPanel" ).panel({
      close: function( event, ui ) {}
    });

    $( "#myPanel" ).on( "panelclose", function( event, ui ) {
        //localStorage.adl = $("#checkbox-h-2a").is(":checked"); //$('#checkbox-h-2a').val();
        if (typeof(Storage) !== "undefined") {
            localStorage.adl = $("#checkbox-h-2a").is(":checked");
            console.log($("#checkbox-h-2a").is(":checked"));
        }
    });
 });
 </script>
 </head> 
 <body> 

<!-- Start of first page -->
<div data-role="page" id="home">
<div data-role="panel" id="myPanel" data-display="overlay">
    <!-- panel content goes here -->
    <fieldset data-role="controlgroup" data-type="horizontal">
    <input name="checkbox-h-2a" id="checkbox-h-2a" type="checkbox">
    <label for="checkbox-h-2a">Auto Detect Location</label>  
  </fieldset>   
</div><!-- /panel -->
<div data-role="header">
    <h1>Test</h1>
    <a href="#myPanel" data-icon="gear" class="ui-btn-right">Open Panel</a>     
</div><!-- /header -->
<div data-role="content">   
    <div data-role="main" class="ui-content">
    <p>content will go here</p>
    </div>
</div><!-- /content -->

<div data-role="footer">
    <h4>Page Footer</h4>
</div><!-- /footer -->     
</div><!-- end of first page -->
</body>
</html>

Answer №1

For optimal results, it is recommended that you utilize the set method offered by localStorage in this manner:

localStorage.setItem(keyName, keyValue);

Feel free to refer to the official documentation for additional details.

Answer №2

It seems that the issue you are encountering is due to accessing or running the HTML file from your local file system instead of a server. This can result in certain functionalities not working as expected, as compared to when accessed from a web server.

I experienced the same problem in IE-11 when loading the page from the file system. Here is a screenshot of the error (note the local file system URL). The error you mentioned regarding localStorage being undefined may be related to this access method.

By contrast, when I accessed the HTML file from a simple HTTP server (as shown in the screenshot below), everything worked correctly. Check out the snapshot where localStorage is defined properly.

To resolve this issue, make sure to run your HTML page from a server environment. You can set up a server using tools like Node.js or a basic Python server. Personally, I used a simple Python server and it solved the problem. Good luck!

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

Error: Attempting to access a property of an undefined value (referencing '8') was unsuccessful

Hello everyone, I am new to posting and identify as a junior Frontend developer. I'm encountering a confusing issue with the InputLabel from Material UI (ver. 5) on a specific section of the website I'm working on. On the homepage, I successfully ...

searching for a refined method to tackle this challenge

I'm relatively new to Angular and I'm interested in finding a more efficient solution for this particular task. The code below functions correctly, but I am uncertain if it is the best approach to achieve the desired outcome. HTML <div ng-a ...

The checkbox event listener becomes dysfunctional when the innerHTML of its container is modified

My current challenge involves creating checkboxes with a blank line inserted after each one. I also need these checkboxes to trigger a function when changed. This is my code snippet: var div = document.getElementById("test"); var cb1 = document.createEl ...

Searching for real-time data with ajax. Strategies for showing alternative results when there is no user input

I am currently working on developing a live search feature using technologies like ajax, jQuery, PHP, and MySQL. The user inputs certain queries which are then sent to form_livesearch.php where the search is processed. I have successfully implemented this ...

Next.JS: The Key to Long-Term Data Storage

How can we retrieve and maintain data during app initialization or user login so that it persists throughout the application? Currently, every time a page is refreshed, the context is cleared and attempting to use getServerSideProps results in undefined ...

Mastering the art of simultaneously running multiple animations

Utilizing two functions to apply a Fade In/Fade Out effect on an element. Confident in the functionality of both functions, as testing them sequentially in the Console proves their effectiveness. However, executing: fadeIn() fadeOut() seems to result in ...

Using the react-router Navigate component within a Next.js application allows for seamless

Is there a way to achieve the same result as the Navigate component in react-router within Nextjs? The next/Router option is available, but I am looking for a way to navigate by returning a component instead. I need to redirect the page without using rout ...

Creating a stylish navigation bar with custom components using Material UI and React

I've been experimenting with the BottomNavigation component from Material UI, but instead of using labels and text, I want to incorporate custom-made ImageButton components. Here's the code snippet from Material UI: import React from 'rea ...

Specialized express Validator for 2 particular fields

I currently have 2 custom validators set up for the fields email and phone. check('phone') .not() .isEmpty() .withMessage('Phone should not be empty') .custom(async phone => { const phoneCheck = await ...

receiving unexpected data while using AJAX

For my PHP project, I have a function that validates a textbox using AJAX. The AJAX is working fine, but it only gives the following output for $return_value==-4 "<br /> <font size='1'><table class='xdebug-error xe-deprecated ...

Is there a way to format wrapped lines with indentation in an unordered list?

Is there a way to indent the wrapped lines of text in an unordered list? The current layout is not quite what I want, as shown in the first image below. Ideally, I would like it to look more like the second image. I attempted to use margin-left: 56px; and ...

res.send() triggers an error of TypeError: Attempting to convert circular structure to JSON

I've encountered an error message that has been discussed before, but none of the proposed solutions seem to work for my situation. My current project involves building a backend express server to manage API requests for a React application. I have c ...

When querying the model, the result may be undefined

I'm encountering an issue where I can't access the content of an array of documents in my model and it's returning undefined. Here is the model structure (Project.js): var mongoose = require('moongoose'); var Schema = mongo ...

What is the best way to transfer Kendo Grid row data to a Kendo popup window within a partial view using jQuery?

I am currently facing a challenge in passing the row data from the Kendo Grid to a partial view that is displayed through a Kendo popup window. Within the partial view, users have the option to upload an image file related to the specific row record. This ...

Displaying threaded discussions in React

Below is an array that contains comments, and I am attempting to display them in a threaded manner by utilizing the parentId property. comments: [ { id: 1, parentId: null }, { id: 2, parentId: 1 }, { id: 3 ...

Unlocking Worldwide Availability for Identifying URL Parameters

Is there a way to obtain global access to the current URL ID params? I am facing difficulty accessing the current URL ID in a child component within React. The ID is essential for querying a MongoDB database in my ChecklistTool component. Typically, I wou ...

Typescript Tooltip for eCharts

I'm working on customizing the tooltip in eChart v5.0.2 using Typescript, but I'm encountering an error related to the formatter that I can't seem to resolve. The error message regarding the function keyword is as follows: Type '(param ...

The view has no access to $scope but the controller does

I need to display the iso code using a $scope when selecting a country from a list. The list of countries is fetched through a $http call in a factory and then stored in a scope. ///////// get countries //////////////// tableFactory.getCountries().then(f ...

A guide on converting a Javascript datetime to a C# DateTime parameter

In a previous question that I asked on StackOverflow, I mentioned that I was able to retrieve my values successfully as the object was no longer null. The object contains a DateTime property named CreatedOn, which I am sending from JavaScript using new Dat ...