JQuery tab Selection problem?

Posted by PeAk on Stack Overflow See other posts from Stack Overflow or by PeAk
Published on 2010-03-24T23:12:53Z Indexed on 2010/03/24 23:23 UTC
Read the original article Hit count: 133

Filed under:

New to JQuery and I was wondering how do I keep any tabbed selected when a user reloads the web page? What part of my code do I need to change?

Here is my JQuery code.

$(document).ready(function() {

    //When page loads...
    $(".form-content").hide(); //Hide all content
    var firstMenu = $("#home-menu ul li:first");
    firstMenu.show();
    firstMenu.find("a").addClass("selected-link"); //Activate first tab
    $(".form-content:first").show(); //Show first tab content

    //On Click Event
    $("#home-menu ul li").click(function() {

        $("#home-menu ul li a").removeClass("selected-link"); //Remove any "selected-link" class
        $(this).find("a").addClass("selected-link"); //Add "selected-link" class to selected tab
        $(".form-content").hide(); //Hide all tab content

        var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the selected-link tab + content
        $(activeTab).fadeIn(); //Fade in the selected-link ID content
        return false;
    });

});

Here is the XHTML code.

<div id="home-menu">
    <ul>
        <li><a href="#personal-info-form" title="Personal Info Form Link">Personal Info</a></li>
        <li><a href="#contact-info-form" title="Contact Info Form Link">Contact Info</a></li>
    </ul>
</div>


<div>

    <div id="personal-info-form" class="form-content">

    <h2>Personal Information</h2>
        <form method="post" action="index.php">
            <fieldset>
                <ul>
                    <li><label for="first_name">First Name: </label><input type="text" name="first_name" id="first_name" size="25" class="input-size" value="<?php if(!empty($first_name)){ echo $first_name; } ?>" /></li>
                    <li><label for="middle_name">Middle Name: </label><input type="text" name="middle_name" id="middle_name" size="25" class="input-size" value="<?php if(!empty($middle_name)){ echo $middle_name; } ?>" /></li>
                    <li><label for="last_name">Last Name: </label><input type="text" name="last_name" id="last_name" size="25" class="input-size" value="<?php if(!empty($last_name)){ echo $last_name; } ?>" /></li>
                    <li><label for="password-1">Password: </label><input type="password" name="password1" id="password-1" size="25" class="input-size" /></li>
                    <li><label for="password-2">Confirm Password: </label><input type="password" name="password2" id="password-2" size="25" class="input-size" /></li>
                    <li><input type="submit" name="submit" value="Save Changes" class="save-button" />
                    <input type="submit" name="submit" value="Preview Changes" class="preview-changes-button" /></li>
                </ul>
            </fieldset>
        </form>

    </div>


    <div id="contact-info-form" class="form-content">

    <h2>Contact Information</h2>
        <form method="post" action="index.php" id="contact-form">
            <fieldset>
                <ul>
                    <li><label for="address">Address 1: </label><input type="text" name="address" id="address" size="25" class="input-size" value="<?php if (isset($_POST['address'])) { echo $_POST['address']; } else if(!empty($address)) { echo $address; } ?>" /></li>
                    <li><label for="address_two">Address 2: </label><input type="text" name="address_two" id="address_two" size="25" class="input-size" value="<?php if (isset($_POST['address_two'])) { echo $_POST['address_two']; } else if(!empty($address_two)) { echo $address_two; } ?>" /></li>
                    <li><label for="city_town">City/Town: </label><input type="text" name="city_town" id="city_town" size="25" class="input-size" value="<?php if (isset($_POST['city_town'])) { echo $_POST['city_town']; } else if(!empty($city_town)) { echo $city_town; } ?>" /></li>
                    <li><input type="submit" name="submit" value="Save Changes" class="save-button" />
                        <input type="hidden" name="contact_info_submitted" value="true" />
                    <input type="submit" name="submit" value="Preview Changes" class="preview-changes-button" /></li>
                </ul>
            </fieldset>

        </form>

    </div>

</div>

© Stack Overflow or respective owner

Related posts about jQuery