I want to display the last three search values on the website using javascript cookies. I store last three values but when I display in view it displays everything together. I have two question.
- I want to display the values separately inside the
href. So the user can click and search again.
Actual output
<a href="abc.com?location=1=Dubai; 2=India; 3=SriLanka">1=Dubai; 2=India; 3=SriLanka <span class='rmvcookie'>x</span></a>
Expecting output
<a href='abc.com?location=Dubai'>Dubai</a><span class='rmvcookie'>x</span>
<a href='abc.com?location=India'>India</a><span class='rmvcookie'>x</span>
<a href='abc.com?location=SriLanka'>SriLanka</a><span class='rmvcookie'>x</span>
- I created
removeCookiesfunction but it removes all the cookies at once. I want to remove the cookies individually.
This website does the same. makemytrip.ae/holidays-india Search by any destination. It stores the values and you can remove it individually. I tried last two days and couldn't get the right code. Can anyone help me? Please.
JavaScript
var num = 1;
function addCookie() {
var searchvalue = document.getElementsByClassName("cookiename")[0].value;
document.cookie = num + "=" + searchvalue;
if (num >= 3) { //Limit for last three search value
num = 1;
} else {
num++;
}
var result = document.cookie;
document.getElementById("list").innerHTML = "<a href='abc.com?location=" + result + "'>" + result + "</a> <span class='rmvcookie' onclick='removeCookies()'>x</span>";
}
function listCookies() {
var result = document.cookie;
document.getElementById("list").innerHTML = "<a href='abc.com?location=" + result + "'>" + result + "</a> <span class='rmvcookie' onclick='removeCookies()'>x</span>";
}
window.onload = function() {
listCookies();
};
function removeCookies() {
var res = document.cookie;
var multiple = res.split(";");
for (var i = 0; i < multiple.length; i++) {
var key = multiple[i].split("=");
document.cookie = key[0] + "=; expires=Thu, 28 May 2030 00:00:00 UTC";
}
}
HTML
<input type="text" id="searchTxt" class="cookiename" name="asd">
<button onclick='addCookie()'>ADD</button><br>
<button onclick='removeCookies()'>REMOVE</button>
<h1>Cookies List</h1>
<p id="list"></p>

GETmethod. So if they click theDubaiallDubaipackages will display. Are there any other ways to do?