Abernasty
Abernasty

Reputation: 670

How can I pull a Cookie's value into a css id statement?

I'm using cookie.js as a means to hold onto info entered into a form's hidden field. There are four different options, and upon each option's being clicked, I want to give the browser a cookie with a value relevant to that option.

These options are hidden by default, but if the user subsequently navigates back after having chosen a value, I want to check for their cookie, then read that cookie's value, take that value as a var, then display the appropriate option as determined from the read cookie.

something like:

if the cookie is set

$.cookie('cookie_name', 'option_number1')

then when someone navigates to the page:

var check_cookie = $.cookie('cookie_name');

    if(check_cookie){
var option_number = $('cookie_name').val();
$(option_number).css("display","block");
}

How can I accomplish this?

Upvotes: 0

Views: 414

Answers (3)

Krish R
Krish R

Reputation: 22721

Can you try this,

$("#"+option_number).show();

Upvotes: 0

Abernasty
Abernasty

Reputation: 670

Since the $.cookie(cookie_name) returned a value in quotes (and returned the value to begin with) those had to be stripped out then the # sign needed to be appended to the front for Id use, after which it could be treated as whatever:

    var check_cookie = $.cookie('cookie_name');
    check_cookie= check_cookie.replace("\"", "");

           alert(check_cookie);
$("#"+check_cookie).css("display","inline-block")

Upvotes: 0

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

Try

$('#' + option_number).css("display","block");

Or simply

$('#' + option_number).show()

Actually you have to prepend a # while using an ID selector. Here is the documentation for the ID selector.

Upvotes: 1

Related Questions