javascript - How to save clicked result in cookie or in localstorage -
$(document).ready(function(){ $(".link").click(function(){ $(".elem").toggle(); }); $('.floating').click(function(evt){ evt.preventdefault(); $('.link').text(event.target.textcontent); // document.cookie = "cookiename=event.target.textcontent"; doesnt working }); }); jquery(function($){ $(document).mouseup(function (e){ var div = $(".link"); var second = $('.elem'); var close = $('.close'); if (!div.is(e.target) && (second.has(e.target).length == 0 || close.is(e.target))) { second.hide(); } }); }); // cookies.set('cookiename','0blue'); one
.wrapper { width: 1180px; margin-right: auto; margin-left: auto; } .elem { display:none; margin-top: 14px; width: 480px; height: 310px; background-color: grey; border-radius: 5px; -moz-border-radius: 5px; -webkit-border-radius: 5px; position: relative; box-shadow:0 2px 3px rgba(0,0,0,0.5); transition: 0.4s; } .elem:after { content: ''; position: absolute; width: 0; height: 0; border-bottom: 10px solid grey; border-left: 10px solid transparent; border-right: 10px solid transparent; top: -10px; left: 33px; } .title { margin: 0px 0px 10px 10px; padding-top: 15px; position: relative; } .link { margin-left: 13px; } .regions { height: 50px; display: inline-block; } .floating { display: inline-block; margin: 10px; line-height: 0.4; width: 20px; } a.floating { text-decoration: none; width: 24%; } a.floating:hover { text-decoration: underline; } .otherregion { margin: 0px 0px 10px 10px; padding-top: 15px; } .edit { border:1px solid #9e9e9e; color: #000000; padding: 3px; font-size: 14px; font-family: verdana; background: #fff; width: 90%; height: 23px; } form { margin: 0px 0px 10px 10px; } .formtext { margin: 0px; padding-top: 2px; } .top { margin-left: 13px; margin-right: 38px; } .close { margin: -27px 0px 20px 444px; position: absolute; font-size: 18px; cursor: pointer; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!doctype html> <html> <head> <title>any project</title> <meta http-equiv="content-type" content="text/html"; charset="utf-8" /> </head> <body> <div class="wrapper"> <a class="link" href="#">countries</a> <div class="elem"> <p class="title">choose youre locality </p><i class="close" >x</i> <div class="regions"> <a class="floating" href="#">kiev</a> <a class="floating" href="#">baku</a> <a class="floating" href="#">paris</a> <a class="floating" href="#">london</a> <a class="floating" href="#">moscow</a> <a class="floating" href="#">amsterdam</a> <a class="floating" href="#">rome</a> <a class="floating" href="#">dubai</a> <a class="floating" href="#">berlin</a> </div> <p class="otherregion">or choose one:</p> <form> <input class="edit" type="text" name="add" placeholder="Начните вводить название"> </form> </div> <script type="text/javascript" src="jquery/jquery-3.2.1.min.js"></script><!-- cookie --> <script type="text/javascript" src="script.js"></script> <script type="text/javascript" src="cookie.js"></script> </body> </html>
first need understand why cookie don't setting in browser. take scripts , run openserver , works great if write code
document.cookie = "cookiename=info123"; alert(document.cookie);
alert shows empty string. , in developer tools chrome show empty line. next need when clicked , choose(clicked) country list of countries must saved in cookie or in localstorage. if it's possible show me please both version, if refresh browser or close browser must there. instead of countries must choice. please week can't solve this.
use localstorage.
$('.floating').click(function(e){ e.preventdefault(); $('.link').text($(this).html()); localstorage.setitem('cookiename',$(this).html()); });
then retrieve it
var value = localstorage.getitem('cookiename');
Comments
Post a Comment