Setting a cookie based on the name of the link that is clicked.

Posted by Ozaki on Stack Overflow See other posts from Stack Overflow or by Ozaki
Published on 2010-05-03T06:22:50Z Indexed on 2010/05/03 6:28 UTC
Read the original article Hit count: 160

Filed under:
|
|

TLDR When clicking on a link I want to assign a cookie with a name of instrument and a value of the text on the link clicked.

Using Jquery.1.4.2.min.js, Jquery.cookie.1.0.js


I am trying to create a cookie when a link is clicked (will always link to "page.html").

name of instrument

value of the TEXT

So far I am trying to use:

Link1:

<a href="page.html">link1</a>

Link2:

<a href="page.html">link2</a>

Script:

$('a[href=page.html]').click(function()
{
var name = 'instrument';
var value = $(this).text();
$.cookie(name, value, { expires: 365 });
});

When I click the link it just loads the link and no cookie is set. Debugging with firebug, firecookie, firequery. No cookie for instrument or anything along the lines is found. Onload I'll hit the

"<a href="page.html">projects</a>" but not the

"$.cookie(name, value, { expires: 365 });" at all.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery