Reputation: 112
Hi am making a webpage in which people click a button and it save cookie by Javascript now what it do is get all cookies and then sort it using which one is clicked
Can anybody have example of something like this or help me in sorting i tried everything but its not working
Like People click on a button -> Save cookie -> then when user Refresh or visit again check cookie and show it to user button sorted
Just a code, Example or Idea how to make this ??
Regards, TicTech
var myc=new Array('<td><img class="dmd" src="gray_star.png" id="1" ><a href="#" class="mlm">Name</a></td>','<td><img class="dmd" id="2" src="gray_star.png"><a href="#" class="mlm">Name</a></td>');
function createCookiecreateCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
$(document).ready(function(){
function getCookies() {
var cookies = { };
if (document.cookie && document.cookie != '') {
var split = document.cookie.split(';');
for (var i = 0; i < split.length; i++) {
var name_value = split[i].split("=");
name_value[0] = name_value[0].replace(/^ /, '');
cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
}
}
return cookies;
}
var myCookies = getCookies();
var em=0;
var $user_table = $("#tb");
var $row1 = $("<tr></tr>");
var $row2 = $("<tr></tr>");
var $row3 = $("<tr></tr>");
var $row4 = $("<tr></tr>");
var $row5 = $("<tr></tr>");
var $row6 = $("<tr></tr>");
var $row7 = $("<tr></tr>");
var ar=new Array();
for(m=1;m<=21;m++){
em=readCookie("vale"+m);
if(em==0){
/*
for(i=1;i<=1;i++){
var e=i;
var m="$row";
var $esm=(eval(m+e)).children();
alert($esm.size());
$esm.each(function(){
//alert($(".dmd").attr('src'));});
*/
$row1.prepend(myc[m-1]);
var $esm=$row1.children();
alert($esm.size());}
else
if(m==1||m==2||m==3){
$row1.append(myc[m-1]);
$user_table.append($row1);
var $esm=$row1.children();
$esm.each(function(){
$(".dmd").attr('src');});
}else if(m==4||m==5||m==6){
var $esm=$row1.children();
$esm.each(function(){
$(".dmd").attr('src');});
//alert(m);
$row2.append(myc[m-1]);
$user_table.append($row2);
}else if(m==7||m==8||m==9){
$row3.append(myc[m-1]);
$user_table.append($row3);
}else if(m==10||m==11||m==12){
$row4.append(myc[m-1]);
$user_table.append($row4);
}else if(m==13||m==14||m==15){
$row5.append(myc[m-1]);
$user_table.append($row5);
}else if(m==16||m==17||m==18){
$row6.append(myc[m-1]);
$user_table.append($row6);
}else if(m==19||m==20||m==21){
$row7.append(myc[m-1]);
$user_table.append($row7);
}}
///$e=$row1.children();
//alert($e.size());
//loop through the menu item
$('.dmd').each(function () {
var id=$(this).attr('id');
if(readCookie('vale'+id)==0){
$(this).attr("src","chamkilla.png");
}else{
$(this).attr("src","gray_star.png");
}
});
$("img.dmd").hover(
function(){
var id=$(this).attr('id');
$(this).attr("src","chamkilla.png");
$(this).click(function () {
if(readCookie('vale'+id)==0){
$(this).attr("src","gray_star.png");
createCookiecreateCookie('vale'+id,'1','1');
}else{
createCookiecreateCookie('vale'+id,'0','7');
$(this).attr("src","chamkilla.png");
}});
},
function(){
$(this).attr("src","gray_star.png");
}
);
});
Upvotes: 1
Views: 192
Reputation: 5010
I am not entirely sure what you mean, without any code or an idea of what exactly it is you want to sort. You say "show it to user button sorted" which suggests you want to sort the button itself - if this is the case then it already is, in ascending and descending order...
With regards to the sorting technique, you probably want to implement a bubble sort.
Please post some code and I will see if I can help you further. A simplified jsfiddle would be good.
Upvotes: 1