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 6838777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:38:16+00:00 2026-05-26T23:38:16+00:00

I have a working example that keeps a div open using cookies. Part of

  • 0

I have a working example that keeps a div open using cookies. Part of the issue is that Im using a select list instead of a radio button. Another problem is that the values of the select list and corresponding div are dynamically generated. However, making things a little easier, the value of the select is ALWAYS the same as the div id. Im just trying to make it so if the page is reloaded, the div stays open. Here is the example that I think is close to what Im looking for:

<fieldset>
<ol class="formset">
  <li>
    <label for="fname2">First Name: </label>`
    <input type="text" id="fname2" value="" name="fname2"/>
  </li>
  <li>
    <label for="lname2">Last Name: </label><br />
    <input type="text" id="lname2" value="" name="lname2"/>
  </li>
  <li>
    <label for="email2">Email Address: </label><br />
    <input type="text" id="email2" value="" name="email2" />
  </li>
  <li>
    <label for="age2">Are you above 21 yrs old?</label><br />
    <input type="radio" name="age2" value="Yes" class="aboveage2" /> Yes
    <input type="radio" name="age2" value="No" class="aboveage2" /> No
  </li>
</ol>
<ol id="parent2" class="formset">
  <li>
    <strong>Parent/Guardian Information:</strong>
  </li>
  <li>
    <label for="pname2">Parent Name: </label>
    <input type="text" id="pname2" value="" name="pname2"/>
  </li>
  <li>
    <label for="contact2">Contact No.: </label><br />
    <input type="text" id="contact2" value="" name="contact2"/>
  </li>
</ol>
<input type="submit" name="submit" value="Submit" class="submitbtn" />  
</fieldset>

<script>
  $(document).ready(function(){
    $("#parent2").css("display","none");
    $(".aboveage2").click(function(){
      if ($('input[name=age2]:checked').val() == "No" ) {
        $("#parent2").slideDown("fast"); //Slide Down Effect
        $.cookie('showTop', 'expanded'); //Add cookie 'ShowTop'
      } else {
        $("#parent2").slideUp("fast");  //Slide Up Effect
        $.cookie('showTop', 'collapsed'); //Add cookie 'ShowTop'
      }
    });
    var showTop = $.cookie('showTop');
    if (showTop == 'expanded') {
      $("#parent2").show("fast");
      $('input[name=age2]:checked');
    } else {
      $("#parent2").hide("fast");
      $('input[name=age2]:checked');
    }
  });
</script>

As a note Im using the cookie plugin found here: http://plugins.jquery.com/project/Cookie Closer to what Im doing and need help with is the following:

<html>
  <head>
    <script>
      $(document).ready(function() {
        $('div.book').css("display","none"); // display none on all ol that doesn't have book class
        $('#book_list').change(function() {
          $('div.book').slideUp("fast"); //Slide Up Effect
          $('#' + $(this).val()).slideDown("slow"); //Slide Down Effect
        });
      });
    </script>
  </head>
  <body>
    <form>
      <select id="book_list">
        <option value="">Select</option>
        <option value="1">Number 1</option>
        <option value="2">Number 2</option>
        <option value="3">Number 3</option>
      </select>
    </form>
    <div id="1" class="book">Div number 1</div>
    <div id="2" class="book">Div number 2</div>
    <div id="3" class="book">Div number 3</div>
  </body>
</html>

I draw the values from a database. This is the PHP I use on reload to keep the menu item selected:

<?php
$bookcookie = $_COOKIE['book'];
$book_list = "<select id=\"book_list\">";
while($book = mysql_fetch_array($book_result)){
//THE BOOK LIST
$book_list .= "<option value=\"" . $book['id'] . "\"";
if(isset($bookcookie) && $bookcookie == $book['id']){
    $book_list .= " selected";
}
$book_list .= ">Number " . $book['id'] . "</option>";
}
$book_list .= "</select>";
?>
  • 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-05-26T23:38:16+00:00Added an answer on May 26, 2026 at 11:38 pm

    I’m a bit loathe to write too much code; if you can provide a solid attempt, hundreds of people will be happy to help you get that last mile!

    I don’t mind giving advice, though.

    Your initial approach isn’t bad at all. Hide all appropriate divs, and then based on change event, show the appropriate one. You just need to insert some new logic:

    • On document ready, check to see if a cookie exists that tells you what div needs to be opened.
    • if the cookie exists, retrieve its value and show the appropriate div
    • if the cooke does not exist, do not open a corresponding div
    • in the change handler, when a user makes a selection, set the cookie and then perform the animation

    Hope that helps!

    [updated per comments below]

    If you want to base it on “selected” being set, you just need to do this:

    $(document).ready(function() {
      $('div.book').css("display","none"); // display none on all divs with class 'book'
      var listVal = $('#book_list').val();
      if(listVal != "") {
        $('#book-'+listVal).show();
      }
    
    
      $('#book_list').change(function() {
        $('div.book').slideUp("fast"); //Slide Up Effect
        $('#book-' + $(this).val()).slideDown("slow"); //Slide Down Effect
      });
    });
    

    Note a change I noticed along the way: your divs can’t be just a number; valid IDs must start with a letter, so I added the pattern “book-” to the beginning. I didn’t post updated HTML, but your divs will need to have IDs book-1 book-2 and book-3 to use my suggested code.

    Not claiming it’s optimized, but that’s the gist. 😉 If no value is selected, the first one is selected by default, which has a value of empty string. One document ready, get the value and if it’s NOT an empty string there must be a div that needs showing. Show it based on the list’s value.

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

Sidebar

Related Questions

Part I I am working on a web application that instead of using a
Does anyone have a working example of a video player built using Qt phonon?
Does anybody have an example of working with database using Visual C++ and OLEDB?
I am working through the examples in a Django book that I have, but
I'm working through the NerdDinner ASP.NET MVC 1.0 example. I have it all working
I have this simple example I can't seems to get working : MERGE INTO
I have been working with Visual Studio (WinForm and ASP.NET applications using mostly C#)
I have been working with a string[] array in C# that gets returned from
I have a Ruby on Rails app that I'm working on and I'm having
I have a script that keeps the row title and column title of a

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.