Ask your JavaScript questions! Pay money and get answers fast! (more info)

Default Selection before Cookie JavaScript

  • REFUNDED

Hello,
I have a draggable div that has three different tabs in it. The tab selection and the position of the div are saved witha cookie after the first visit. My problem is that when the page loads, it loads without a tab selected (waiting for the user to pick one). The page then looks blank and people are confused. Is there any way to select one tab on first load but then overwrite that selection once the cookie is created?

I forgot to mention that the tabs are calling up other html pages via AJAX.

/* Applying active class to selected Item */

if ($.cookie('activePage') != null) {
/* Create variables ~
- activePage is the cookie, which is the index of the link in the list
- activePageHTML is the data-href for that particular anchor tag */
var activePage = $.cookie('activePage'),
activePageHTML = $('#tbox1 a:eq(' + activePage + ')').attr('data-href');

/* Handle the Selected Item in the Menu */
$('#tbox1 a').removeClass('bold');
$('#tbox1 a:eq(' + activePage + ')').addClass('bold');

/* Handle the AJAX Requests */
$.get(activePageHTML, function(html){
$('#contentarea').html(html);
});

}

Answers (2)

2013-06-03

Utkarsh Kukreti answers:

Just set activePage to a default value if the cookie returns null.

Replace "DEFAULT VALUE" with the value you want activePage to have if no cookie exists.

/* Applying active class to selected Item */

/* Create variables ~
- activePage is the cookie, which is the index of the link in the list
- activePageHTML is the data-href for that particular anchor tag */
var activePage = $.cookie('activePage') || "DEFAULT VALUE",
activePageHTML = $('#tbox1 a:eq(' + activePage + ')').attr('data-href');

/* Handle the Selected Item in the Menu */
$('#tbox1 a').removeClass('bold');
$('#tbox1 a:eq(' + activePage + ')').addClass('bold');

/* Handle the AJAX Requests */
$.get(activePageHTML, function(html){
$('#contentarea').html(html);
});


Noahsvan comments:

No Dice. I get pages and the background starts blinking.
Maybe you need to see it in context. I'm setting up a link.

[[LINK href="noahvenezia.com/t3st3er/book3/home_009.html"]][[/LINK]]


Noahsvan comments:

No Dice. I get pages and the background starts blinking.
Maybe you need to see it in context. I'm setting up a link.

[[LINK href="noahvenezia.com/t3st3er/book3/home_009.html"]][[/LINK]]


Noahsvan comments:

[[LINK href="http://noahvenezia.com/T3st3r/book3/home_009.html"]] HERE [[/LINK]]

2013-06-03

mohitseth answers:

R u using jQueryUI?

If you are using it, then use "selected" option for jQueryUI < 1.9 and "active" option for jQueryUI >=1.9.

For reference you can look at the API for other options:

http://api.jqueryui.com/tabs/