Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stay on a current tab after after submitting a form using jQuery

Tags:

jquery

I'm trying to stay on a specif tab ( in this case tab2 ) but after clicking the submit button the tab reverts to tab 1 which has the default class of active. I have hit a brick wall with this one.

Here is my example [jsfiddle]:

HTML:

<ul class='tabs'>
            <li><a href='#tab1'>Tab 1</a></li>
            <li><a href='#tab2'>Tab 2</a></li>
        </ul>
<div id='tab1'>
            <h3>Section 1</h3>
            <p>Lorem ipsum dolor sit amet, consssectetur adipiscing elit. Donec lobortis placerat dolor id aliquet. Sed a orci in justo blandit commodo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae.</p>
                <form id="form1" action="" method="post">            
      <input id="button-1" name="" type="submit" />
</form>
        </div>
<div id='tab2'>
<a name="tab2"></a>
            <h3>Section 2</h3>
            <p>Aenean et est tortor. In pharetra pretium convallis. Mauris sollicitudin ligula non mi hendrerit varius. Fusce convallis hendrerit mauris, eu accumsan nisl aliquam eu.</p>

    <form id="form2" action="" method="post">            
      <input id="button-2" name="" type="submit" />
</form>
        </div>
<div>
  <h2>RESULTS </h2>
</div>        

JavaScript:

$(document).ready(function(){
    $('ul.tabs').each(function(){
        var $active, $content, $links = $(this).find('a');
        $active = $links.first().addClass('active');
        $content = $($active.attr('href'));
        $links.not(':first').each(function () {
            $($(this).attr('href')).hide();
        });

        $(this).on('click', 'a', function(e){
            $active.removeClass('active');
            $content.hide();
            $active = $(this);
            $content = $($(this).attr('href'));
            $active.addClass('active');
            $content.show();
            e.preventDefault();
        });
    });
});

​ CSS:

*         {padding:0; margin:0;}
html      {padding:15px 15px 0;font-family:sans-serif;font-size:14px;}
p, h3     {margin-bottom:15px;}
div       {padding:10px;width:600px;background:#fff;}
ul.tabs   {margin-left:2px;}
#tab1, #tab2 {border-top: solid 1px #ccc;margin-top: -1px; }
#tab2      {display:none;}a
ul.tabs li  {list-style:none;display:inline;}
ul.tabs li a   {padding:5px 10px;display:inline-block;background:#666;color:#fff;text-decoration:none;}
ul.tabs li a.active {background:#fff;color:#000;border: solid 1px #ccc; border-width: 1px 1px 0 1px;}

Your help would be greatly appreciated

like image 540
NickP Avatar asked Jul 19 '12 12:07

NickP


People also ask

How do I go to a specific tab in jquery?

click(function () { // bind click event to link $tabs. tabs('select', 4); // switch to tab return false; });

How do I make my current tab active in HTML?

With the help of HTML, CSS, and JavaScript, it is possible to create a navigation menu with a curved active tab. This can be done by using the ::before and ::after pseudo-elements to create the desired shape, and then using JavaScript to add the active class to the element.


3 Answers

Try this

<asp:HiddenField ID="hidAccordionIndex" runat="server" Value="0" />

Put this in your coding and change the script as follows

$("#accordion").accordion({
header: "h3",
autoHeight: false,
event: "mousedown",
active: activeIndex,
ui: "PageName.aspx",
change: function (event, ui) {
var index = $(this).accordion("option", "active");
$('#<% =hidAccordionIndex.ClientID %>').val(index);
}
});
like image 139
Madusanka Avatar answered Oct 29 '22 13:10

Madusanka


<div id="tab">
<ul>
            <li><a href='#tab1'>Tab 1</a></li>
            <li><a href='#tab2'>Tab 2</a></li>
        </ul>
</div>
<form id="form1" action="?tab=tab1" method="post"> 

The jQuery function:
$(document).ready(function(){
        $("#tab").tabs();
        var param = $(document).getUrlParam('tab');
        if (param !=null)
            $("#tab").tabs('select',param);
        else 
            $("#tab").tabs();
    });
like image 25
Neha Avatar answered Oct 29 '22 12:10

Neha


You can specify tab in URL. A tab is an anchor in a URL like: yourURL/#tab

<form id="form2" action="#tab2" method="post">            

</form>
like image 1
gopeca Avatar answered Oct 29 '22 12:10

gopeca