/    Sign up×
Community /Pin to ProfileBookmark

Session storage

Hi,

I have a project where I want visitors to a site to pick the Tariff that is appropriate to them and then I will display relevant content to them while hiding the drop down to change between tariffs. I would like this selection to stay over multiple pages but then reset when they close the browser and start again.

So far I have managed to show multiple hidden divs based on a selection and then hide the selection box itself. I may have managed to save the selection as a session, however I am struggling to work out how to recall that saved information on a page refresh. I also assume that once I can recall it for a page refresh I can then apply the same code to different pages in my site which have the same drop down (as you never know where people will start their browsing) and it will remember the selection.

In my example I am expecting to need to save a selection i.e. 3 that has the value t3 and then recall this saved value if it exists on page refresh to retain the selection of 3 and keep those divs displayed etc…

I am very new to javascript so be gentle with me, here is my fiddle https://jsfiddle.net/k33npx/cbe36aq5/8/

I hope that all makes sense and thanks in advance for your help

to post a comment

2 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinOct 21.2021 — @k33npx#1638400

``<i>
</i>&lt;style&gt;
.t1
{display: none}

.t2
{display: none}

.t3
{display: none}

.t4
{display: none}

.t5
{display: none}
&lt;/style&gt;

&lt;h4&gt;Your Tariff&lt;/h4&gt;


&lt;div&gt;
&lt;br&gt;

&lt;select class="select" id="select"&gt;
&lt;option&gt;Choose Your Development&lt;/option&gt;
&lt;option name="Tarrif 1" value="t1"&gt; 1&lt;/option&gt;
&lt;option name="Tarrif 2" value="t2"&gt; 2&lt;/option&gt;
&lt;option name="Tarrif 3" value="t3"&gt; 3&lt;/option&gt;
&lt;option name="Tarrif 4" value="t4"&gt; 4&lt;/option&gt;

&lt;option name="Tarrif 5" value="t5"&gt; 52&lt;/option&gt;

&lt;/select&gt;

&lt;br&gt;
&lt;span id="current"&gt;&lt;/span&gt;
&lt;div class="t1"&gt;&lt;p&gt;Tariff 1......&lt;/p&gt;&lt;/div&gt;
&lt;div class="t2"&gt;&lt;p&gt;Tariff 2......&lt;/p&gt;&lt;/div&gt;
&lt;div class="t3"&gt;&lt;p&gt;Tariff 3.....&lt;/p&gt;&lt;/div&gt;
&lt;div class="t3"&gt;&lt;p&gt;Tariff 3 extra information&lt;/p&gt;&lt;/div&gt;
&lt;div class="t4"&gt;&lt;p&gt;Tariff 4......&lt;/p&gt;&lt;/div&gt;
&lt;div class="t5"&gt;&lt;p&gt;Tariff 5......&lt;/p&gt;&lt;/div&gt;
&lt;div class="t5"&gt;&lt;p&gt;Some more information&lt;/p&gt;&lt;/div&gt;
&lt;script&gt;
function showSelectedItem() {
var itemSelect = document.getElementById("select");
var description = itemSelect.options[itemSelect.selectedIndex].text;
document.getElementById("current").textContent = description;
}
function setDisplay(className, displayValue) {
var items = document.getElementsByClassName(className);
for (var i=0; i &lt; items.length; i++) {
items[i].style.display = displayValue;
}
}
function showbox() {
var itemSelect = document.getElementById("select");
var boxName = itemSelect.options[itemSelect.selectedIndex].value;
setDisplay(boxName, "block");
setDisplay("select", "none");
}
function setItem(){
var itemSelect = document.getElementById("select");
var itemSelect = itemSelect.options[itemSelect.selectedIndex].value;
sessionStorage.setItem("itemSelect", itemSelect);
}
document.getElementById("select").addEventListener("change",() =&gt; {
showSelectedItem();
setDisplay();
showbox();
setItem();
});
var item = sessionStorage.getItem("itemSelect");
if(item)document.getElementsByClassName(item)[0].style.display='block';
&lt;/script&gt;<i>
</i>
``
Copy linkTweet thisAlerts:
@AloizychOct 23.2021 — Did the advice help you?
×

Success!

Help @k33npx spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...