Hi Bhaarat,

I'm not sure what the setCookie() function, but I'm guessing that the problem lies in what you're doing inside $('#menu li a').click(). It looks like you're attaching a click handler to all of the menu links that attaches a click handler to the clicked link. A simplification of your code should help. You have this ...

$('#menu li a').click(
   function() {
   $(this).click(function(){setCookie($(this).attr('class'));});
   alert('this isname: ' + $(this).attr('name'));
       $(this).next().slideToggle('normal');
     }
   );


Try this instead...

$('#menu li a').click(
   function() {
       setCookie($(this).attr('class'));
       alert('this isname: ' + $(this).attr('name'));
       $(this).next().slideToggle('normal');
     }
   );

Also, since it appears that you are using the cookie plugin, you could replace the setCookie() line with this:

$.cookie('the_cookie', this.className);

Hope that helps,

--Karl Swedberg



On Jan 29, 2008, at 11:01 AM, Bhaarat Sharma wrote:


Hi

I have the following snippet to expand/collapse menu items and set the
cookie as well.

   function initMenu() {
 $('#menu ul').hide();
if ($.cookie('the_cookie')=='a'||$.cookie('the_cookie')=='b'||
$.cookie('the_cookie')=='c'||
       $.cookie('the_cookie')=='d')
 {
     alert('came here with cookie: ' + $.cookie('the_cookie'));
       $("a").filter("." +
$.cookie('the_cookie')).next().slideDown('normal');
     alert('did the sliding: ' + $("a").filter(".b").attr('name'));
 }

 $('#menu li a').click(
   function() {
   $(this).click(function(){setCookie($(this).attr('class'));});
   alert('this isname: ' + $(this).attr('name'));
       $(this).next().slideToggle('normal');
     }
   );
 }
 function setCookie(some)
 {
   $.cookie('the_cookie', some);
   alert('cookie set  ' + some);
 }
 $(document).ready(function() {initMenu();});

as you see. I am trying to set the cookie every time that user clicks
the menu item.  However, the cookie is only being set the SECOND time
user clicks the menu item.
First click - menu item expands
Second click - cookie is set and then menu item collapse.

Why is this? when I have the code inside the click.

I thought maybe becuase i am doing this:
$(this).click(function(){setCookie($(this).attr('class'));});

so just wondering..will it be better if I can just assign a function
to $(this) rather than on 'click'

Thanks

Reply via email to