How to store links for selected images from an Array to the localstorage










0















I am looking for a way to store selected images by storing their index in the array. Once the index of the selected images is saved, and the button (get item (with the id of "done") is clicked, I need to stringify the images selected in the array so to store them in the localstorage. Then I need to extract the data from the localstorage and convert it back to an object, write the selected images on the page, and import the selected images from the local storage on the page.
The page should work if changes are made to the length of the array.



Also, I need to use the checkbox to select or deselect the item, but at the moment it's only working when I click on the image. Thank you! Please only javascript



 <html>
<head>
<style>
img.selected opacity:0.5;
</style>
</head>

<body onload="vegetable()">
<button id="done" onclick="new()">get item </button>
<div id="idmain"></div>
<script>
function new()
var hey = [
item: "knife", fruit: "banana.png" ,
item: "fork", fruit: "apple.png" ,
item: "sword", fruit: "kiwi.jpg" ,
item: "spoon", fruit: "mango.jpg"
];

function vegetable ()
for (var i = 0; i < hey.length; i++)
var Img = document.createElement("img");
Img.setAttribute("src", hey[i].fruit);
Img.setAttribute("class", "posterImage");

var Div = document.createElement("div");
Div.setAttribute("class", "posterDiv");
Img.addEventListener("click", function()
if (this.parentNode.getElementsByTagName("input")[0].checked)
this.className = "posterImage unselected";
this.parentNode.getElementsByTagName("input")[0].checked = false;
else
this.className = "posterImage selected";
this.parentNode.getElementsByTagName("input")[0].checked = true;

);
Div.appendChild(Img);
var newCheckbox = document.createElement("input");
newCheckbox.setAttribute("type", "checkbox");
Div.appendChild(newCheckbox);
document.getElementById("idmain").appendChild(Div);


</script>
</body>
</html>









share|improve this question
























  • localStorage has size restrictions. Why would you need to store whole images?

    – charlietfl
    Nov 14 '18 at 18:09











  • No, I don't want to store the whole image, only the url

    – hey
    Nov 14 '18 at 18:12











  • OK I misunderstood what "I need to stringify the images selected" meant

    – charlietfl
    Nov 14 '18 at 18:15






  • 1





    Can you be a little more specific about where you are running into problems? It sounds like you already understand how to store the data - but perhaps not?

    – charlietfl
    Nov 14 '18 at 18:18
















0















I am looking for a way to store selected images by storing their index in the array. Once the index of the selected images is saved, and the button (get item (with the id of "done") is clicked, I need to stringify the images selected in the array so to store them in the localstorage. Then I need to extract the data from the localstorage and convert it back to an object, write the selected images on the page, and import the selected images from the local storage on the page.
The page should work if changes are made to the length of the array.



Also, I need to use the checkbox to select or deselect the item, but at the moment it's only working when I click on the image. Thank you! Please only javascript



 <html>
<head>
<style>
img.selected opacity:0.5;
</style>
</head>

<body onload="vegetable()">
<button id="done" onclick="new()">get item </button>
<div id="idmain"></div>
<script>
function new()
var hey = [
item: "knife", fruit: "banana.png" ,
item: "fork", fruit: "apple.png" ,
item: "sword", fruit: "kiwi.jpg" ,
item: "spoon", fruit: "mango.jpg"
];

function vegetable ()
for (var i = 0; i < hey.length; i++)
var Img = document.createElement("img");
Img.setAttribute("src", hey[i].fruit);
Img.setAttribute("class", "posterImage");

var Div = document.createElement("div");
Div.setAttribute("class", "posterDiv");
Img.addEventListener("click", function()
if (this.parentNode.getElementsByTagName("input")[0].checked)
this.className = "posterImage unselected";
this.parentNode.getElementsByTagName("input")[0].checked = false;
else
this.className = "posterImage selected";
this.parentNode.getElementsByTagName("input")[0].checked = true;

);
Div.appendChild(Img);
var newCheckbox = document.createElement("input");
newCheckbox.setAttribute("type", "checkbox");
Div.appendChild(newCheckbox);
document.getElementById("idmain").appendChild(Div);


</script>
</body>
</html>









share|improve this question
























  • localStorage has size restrictions. Why would you need to store whole images?

    – charlietfl
    Nov 14 '18 at 18:09











  • No, I don't want to store the whole image, only the url

    – hey
    Nov 14 '18 at 18:12











  • OK I misunderstood what "I need to stringify the images selected" meant

    – charlietfl
    Nov 14 '18 at 18:15






  • 1





    Can you be a little more specific about where you are running into problems? It sounds like you already understand how to store the data - but perhaps not?

    – charlietfl
    Nov 14 '18 at 18:18














0












0








0


1






I am looking for a way to store selected images by storing their index in the array. Once the index of the selected images is saved, and the button (get item (with the id of "done") is clicked, I need to stringify the images selected in the array so to store them in the localstorage. Then I need to extract the data from the localstorage and convert it back to an object, write the selected images on the page, and import the selected images from the local storage on the page.
The page should work if changes are made to the length of the array.



Also, I need to use the checkbox to select or deselect the item, but at the moment it's only working when I click on the image. Thank you! Please only javascript



 <html>
<head>
<style>
img.selected opacity:0.5;
</style>
</head>

<body onload="vegetable()">
<button id="done" onclick="new()">get item </button>
<div id="idmain"></div>
<script>
function new()
var hey = [
item: "knife", fruit: "banana.png" ,
item: "fork", fruit: "apple.png" ,
item: "sword", fruit: "kiwi.jpg" ,
item: "spoon", fruit: "mango.jpg"
];

function vegetable ()
for (var i = 0; i < hey.length; i++)
var Img = document.createElement("img");
Img.setAttribute("src", hey[i].fruit);
Img.setAttribute("class", "posterImage");

var Div = document.createElement("div");
Div.setAttribute("class", "posterDiv");
Img.addEventListener("click", function()
if (this.parentNode.getElementsByTagName("input")[0].checked)
this.className = "posterImage unselected";
this.parentNode.getElementsByTagName("input")[0].checked = false;
else
this.className = "posterImage selected";
this.parentNode.getElementsByTagName("input")[0].checked = true;

);
Div.appendChild(Img);
var newCheckbox = document.createElement("input");
newCheckbox.setAttribute("type", "checkbox");
Div.appendChild(newCheckbox);
document.getElementById("idmain").appendChild(Div);


</script>
</body>
</html>









share|improve this question
















I am looking for a way to store selected images by storing their index in the array. Once the index of the selected images is saved, and the button (get item (with the id of "done") is clicked, I need to stringify the images selected in the array so to store them in the localstorage. Then I need to extract the data from the localstorage and convert it back to an object, write the selected images on the page, and import the selected images from the local storage on the page.
The page should work if changes are made to the length of the array.



Also, I need to use the checkbox to select or deselect the item, but at the moment it's only working when I click on the image. Thank you! Please only javascript



 <html>
<head>
<style>
img.selected opacity:0.5;
</style>
</head>

<body onload="vegetable()">
<button id="done" onclick="new()">get item </button>
<div id="idmain"></div>
<script>
function new()
var hey = [
item: "knife", fruit: "banana.png" ,
item: "fork", fruit: "apple.png" ,
item: "sword", fruit: "kiwi.jpg" ,
item: "spoon", fruit: "mango.jpg"
];

function vegetable ()
for (var i = 0; i < hey.length; i++)
var Img = document.createElement("img");
Img.setAttribute("src", hey[i].fruit);
Img.setAttribute("class", "posterImage");

var Div = document.createElement("div");
Div.setAttribute("class", "posterDiv");
Img.addEventListener("click", function()
if (this.parentNode.getElementsByTagName("input")[0].checked)
this.className = "posterImage unselected";
this.parentNode.getElementsByTagName("input")[0].checked = false;
else
this.className = "posterImage selected";
this.parentNode.getElementsByTagName("input")[0].checked = true;

);
Div.appendChild(Img);
var newCheckbox = document.createElement("input");
newCheckbox.setAttribute("type", "checkbox");
Div.appendChild(newCheckbox);
document.getElementById("idmain").appendChild(Div);


</script>
</body>
</html>






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 19:50









KevinO

3,10131730




3,10131730










asked Nov 14 '18 at 18:04









heyhey

1




1












  • localStorage has size restrictions. Why would you need to store whole images?

    – charlietfl
    Nov 14 '18 at 18:09











  • No, I don't want to store the whole image, only the url

    – hey
    Nov 14 '18 at 18:12











  • OK I misunderstood what "I need to stringify the images selected" meant

    – charlietfl
    Nov 14 '18 at 18:15






  • 1





    Can you be a little more specific about where you are running into problems? It sounds like you already understand how to store the data - but perhaps not?

    – charlietfl
    Nov 14 '18 at 18:18


















  • localStorage has size restrictions. Why would you need to store whole images?

    – charlietfl
    Nov 14 '18 at 18:09











  • No, I don't want to store the whole image, only the url

    – hey
    Nov 14 '18 at 18:12











  • OK I misunderstood what "I need to stringify the images selected" meant

    – charlietfl
    Nov 14 '18 at 18:15






  • 1





    Can you be a little more specific about where you are running into problems? It sounds like you already understand how to store the data - but perhaps not?

    – charlietfl
    Nov 14 '18 at 18:18

















localStorage has size restrictions. Why would you need to store whole images?

– charlietfl
Nov 14 '18 at 18:09





localStorage has size restrictions. Why would you need to store whole images?

– charlietfl
Nov 14 '18 at 18:09













No, I don't want to store the whole image, only the url

– hey
Nov 14 '18 at 18:12





No, I don't want to store the whole image, only the url

– hey
Nov 14 '18 at 18:12













OK I misunderstood what "I need to stringify the images selected" meant

– charlietfl
Nov 14 '18 at 18:15





OK I misunderstood what "I need to stringify the images selected" meant

– charlietfl
Nov 14 '18 at 18:15




1




1





Can you be a little more specific about where you are running into problems? It sounds like you already understand how to store the data - but perhaps not?

– charlietfl
Nov 14 '18 at 18:18






Can you be a little more specific about where you are running into problems? It sounds like you already understand how to store the data - but perhaps not?

– charlietfl
Nov 14 '18 at 18:18













0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306307%2fhow-to-store-links-for-selected-images-from-an-array-to-the-localstorage%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306307%2fhow-to-store-links-for-selected-images-from-an-array-to-the-localstorage%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto