Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript show last tab

I've created 4 tabs and only 1 is shown at a time, but if I post something in a form in tab 4 then tab 1 becomes active but I want it to automatically show the tab that the stuff was posted in.

Here is my code:

<script src="../js/jquery-1.8.2.min.js" type="text/javascript"></script>
<script type="text/javascript">

$(document).ready(function() {

    //Default Action
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li:first").addClass("active").show(); //Activate first tab
    $(".tab_content:first").show(); //Show first tab content
    
    //On Click Event
    $("ul.tabs li").click(function() {
        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content
        var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active content
        return false;
    });

});
</script>


<center>
  <ul class="tabs">
    <li><a href="#tab1">Tab 1</a></li>
    <li><a href="#tab2">Tab 2</a></li>
    <li><a href="#tab3">Tab 3</a></li>
    <li><a href="#tab4">Tab 4</a></li>

  </ul>
  <div class="tab_container"><br><br><br>
    <div id="tab1" class="tab_content">
<center><h2>Tab 1</h2></center>
                </div>
                    <div id="tab2" class="tab_content">
<center><h2>Tab 2</h2></center>
                </div>
                    <div id="tab3" class="tab_content">
<center><h2>Tab 3</h2></center>
                </div>
                    <div id="tab4" class="tab_content">
                    <?php
                    $getmessage = $_POST["message"];
                    if($getmessage != null) {
                        echo"Thanks for posting the text!";
                    } ?>
<center><form method="post" name="form4"><input type="text" id="form4" name="message" placeholder="Type a text here"><br><input type="submit" value="Post text"></form></center>
                </div>
</div></center>
</center>

So when I submit the form in tab 4 I get to tab 1 and I manually have to go to tab 4 to see the message that my echo shows. I need it to automatic go to the tab where the form was submitted, but I don't know how to do this.

like image 675
Patric Nøis Avatar asked Sep 24 '26 18:09

Patric Nøis


1 Answers

Add another input (hidden) element to your form, such as active_tab and populate it with the active tab as and when you click on the tabs. Grab that value from $_POST and set the default tab accordingly.

Step 1: add input element

<input type="hidden" name="active_tab" />

Step 2: update input's value

$("ul.tabs li").click(function() {
    $('input[name="active_tab"]').val( $(this).index() );
    //your remaining code as is
    //...

Step 3: set default tab based on post value

$(document).ready(function() {
    //Default Action
    var tabNum = "<? echo !empty($_POST['active_tab']) ? $_POST['active_tab'] : 0; ?>";
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li").eq(tabNum).addClass("active").show(); //Activate the tab
    $(".tab_content").eq(tabNum).show(); //Show the tab content
like image 167
lshettyl Avatar answered Sep 28 '26 19:09

lshettyl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!