Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7806823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:39:21+00:00 2026-06-02T02:39:21+00:00

My previous script here worked in hiding and displaying certain divs when called but

  • 0

My previous script here worked in hiding and displaying certain divs when called but i want the cookie to remember which one was to be shown so users dont have to continually click the link on every page but so far i have failed in getting the jquery.cookie plug to work with my coding, any help would be great.

working single page:
old code jquery:

function language(chosenlanguage) {
 $('div[name|="lang"]').each(function(index) {
      if ($(this).attr("class") == chosenlanguage) {
           $(this).show(0);
      }
      else {
           $(this).hide(0);
      }
 });
}

old code html (abreviated with only a href and div):

<div class="language">
<a href="javascript:language('english');">english</a>
<a href="javascript:language('spanish');">spanish</a>
<a href="javascript:language('german');">german</a>
</div>
<div name="lang" class="english" style="display:block;">content</div>
<div name="lang" class="spanish" style="display:none;">content</div>
<div name="lang" class="german" style="display:none;">content</div>

My new code using the jquery.cookie.js plugin is as shown but it does absolutely nothing.

jquery with cookie script:

$(document).ready(function() { 
if($.cookie("language")) {
    $('div[name|="lang"]').each(function(index) {
      if ($(this).attr("class") == $.cookie("language")) {
           $(this).show(0);
      }
      else {
           $(this).hide(0);
      }
 });
}
$(".language a").click(function() { 
    $('div[name|="lang"]').each(function(index) {
      if ($(this).attr("class") == $(".language a").attr("name")) {
           $(this).show(0);
      }
      else {
           $(this).hide(0);
      }
 });
$.cookie("language",$('.language a').attr('name'), {expires: 365, path:      '/'});
    return false;
});
});

html(abreviated):

<div class="language">
<a href="javascript:language('english');" name="english">english</a>
<a href="javascript:language('spanish');" name="spanish">spanish</a>
<a href="javascript:language('german');" name="german">german</a>
</div>
<div name="lang" class="english" style="display:block;">content</div>
<div name="lang" class="spanish" style="display:none;">content</div>
<div name="lang" class="german" style="display:none;">content</div>

So i’m just wondering what i’m doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:39:23+00:00Added an answer on June 2, 2026 at 2:39 am
    $.cookie("language",$('.language a').attr('name'), {expires: 365, path:      '/'});
        return false;
    });
    

    always sets the value of the cookie to english since that always returns the name of the first <a /> in .languages

    also, you shouldn’t compare classes to strings this way: .attr('class') == lang, you should instead use hasClass, eg: .hasClass(lang)

    $(".language a").click(function() { 
        $('div[name|="lang"]').each(function(index) {
          if ($(this).attr("class") == $(".language a").attr("name")) {
               $(this).show(0);
          }
          else {
               $(this).hide(0);
          }
     });
    

    should look like:

    $(".language a").click(function() { 
        var clicked = this;
        $('div[name|="lang"]').each(function(index) {
          if ($(this).hasClass($(clicked).attr("name"))) {
               $(this).show();
          }
          else {
               $(this).hide();
          }
        });
        $.cookie("language", $(clicked).attr('name'), {expires: 365, path: '/'});
    });
    

    notice i am saving the clicked item in a variable before looping through the divs, you need to do that to have a reference to the clicked item

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Although it is looking similar to my previous post but here purpose is different.
This is related to my previous post here . Running the actual script, I
Since my previous post was closed, but the problem remains, I'll rephrase it here.
I have a script that displays one div at a time while hiding the
(This question is following on from my previous problem which has been fixed (Here)
I'm using the following script in my website in order to create pagination next-previous
I am writing an ajax script in jQuery. The script gets new or previous
When running commands from a bash script, does bash always wait for the previous
In previous versions of Visual Studio I remember I saw the error code/number in
So here's the scenario. I've got a script that runs some tests. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.