0

Based on the advice found in this Stack Overflow q/a I'm attempting to trigger a click via a cookie. The cookie's being set on a radio button selection. One of the two divs being toggled need to be set to hidden.

The problem: The initial if statement isn't triggering a click.

if($.cookie('light-or-dark')) {
    $($.cookie('light-or-dark') + '-radio').attr('checked', true).trigger('click');
    } else {
    $('#theme-schemes-dark').hide();
  };

// the toggle on radio button change function
$('[name=light-dark-radio]').change(function(){
    $('#theme-schemes-light').toggle('medium');
    $('#theme-schemes-dark').toggle('medium');

    // set cookie on toggle state
    $.cookie('light-or-dark', '#theme-schemes-' + this.value, {expires:365, path: '/'})
  })

The HTML:

<div class="selection-wrap">
  <label>
    <input type="radio" name="light-dark-radio" id="theme-schemes-light-radio" value="light" checked>
    Light
  </label>
  <label>
    <input type="radio" name="light-dark-radio" id="theme-schemes-dark-radio" value="dark">
    Dark
  </label>
</div>

<!-- first div -->
<div class="selection-wrap" id="theme-schemes-light">
  <a class="theme-scheme-btn color-box bg-blue" id="defaultThemeScheme" href="#">Light 1</a>
  <a class="theme-scheme-btn color-box bg-yellow" href="#">Light 2</a>
</div>

<!-- second div - hidden by default -->          
<div class="selection-wrap" id="theme-schemes-dark" style="display: none">
  <a class="theme-scheme-btn color-box dark-color-blue" href="#">Dark 1</a>
  <a class="theme-scheme-btn color-box dark-color-yellow" href="#" >Dark 2</a>
</div>

A FIDDLE to better illustrate

Much obliged for insight.

Community
  • 1
  • 1
Isaac Gregson
  • 1,999
  • 1
  • 21
  • 31

2 Answers2

0

I dont seem to have the same problem. ( http://jsfiddle.net/jaLQZ/3/ )

  $($.cookie('light-or-dark') + '-radio').click(function(){
    alert( 'click' + this.value );
  });

  if($.cookie('light-or-dark')) {
    $($.cookie('light-or-dark') + '-radio').attr('checked', true).trigger('click');
    } else {
    $('#theme-schemes-dark').hide();
  };

  $('[name=light-dark-radio]').change(function(){
    $('#theme-schemes-light').toggle('medium');
    $('#theme-schemes-dark').toggle('medium');
    // set cookie on toggle state
    $.cookie('light-or-dark', '#theme-schemes-' + this.value, {expires:365, path: '/'});
  })

Maybe you are using a browser that doesnt support the cookie plugin?

Kapi
  • 547
  • 5
  • 11
  • no, i'm using the latest Chrome and Cookies are certainly working. The problem's that when switching pages the toggled div reverts to the default (light), even if dark is selected before clicking to another page. So it seems that for some reason the radio isn't being clicked via the initial `if` statement. – Isaac Gregson Mar 31 '14 at 14:33
0

Try this, I change code for restore div's

  if($.cookie('light-or-dark')) {

$($.cookie('light-or-dark') + '-radio').attr('checked', true);
   $('#theme-schemes-light').hide();
   $('#theme-schemes-dark').hide();
   $($.cookie('light-or-dark')).show();
} else {
$('#theme-schemes-dark').hide();
};

$('[name=light-dark-radio]').change(function(){
$('#theme-schemes-light').toggle('medium');
$('#theme-schemes-dark').toggle('medium');
// set cookie on toggle state
$.cookie('light-or-dark', '#theme-schemes-' + this.value, {expires:365, path: '/'});
})
Łukasz Szewczak
  • 1,851
  • 1
  • 13
  • 14