Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: Active tab bold

  1. #11
    Administrator Frinkky's Avatar
    Join Date
    Dec 2008
    Posts
    1,817
    Blog Entries
    1

    Default 18 Apr 2012 @ 21.09

    Assuming the following is your tab structure:
    HTML Code:
    <ul>
        <li id="tab1" class="tab"></li>
        <li id="tab2" class="tab"></li>
        <li id="tab3" class="tab"></li>
        <li id="tab4" class="tab"></li>
    </ul>
    When one is clicked, javascript adds a class of 'active' to that li, like so:
    HTML Code:
    <ul>
        <li id="tab1" class="active tab"></li> <!-- this is the active tab now -->
        <li id="tab2" class="tab"></li>
        <li id="tab3" class="tab"></li>
        <li id="tab4" class="tab"></li>
    </ul>
    The tab with the two classes, 'active' and 'tab' will pick up the css from:
    Code:
    .active.tab {
    font-weight: bold
    }
    You'll notice that '.active.tab' has no space so it isn't matching a child '.tab' with a parent '.active', just an element that has those two classes.

    The javascript also needs to remove the class active from the current tab when another tab is clicked, thereby making the new tab the sole active tab.

    The javascript, or jQuery in this case, would be something like:

    Code:
    $('.tab').live('click', function() {
        $(this).addClass('active');
        $(this).siblings().removeClass('active');
    });
    A live example: http://jsfiddle.net/frinkky/jL2fG/
      Reply With Quote

  2. #12
    Member Jacob's Avatar
    Join Date
    Feb 2009
    Posts
    247

    Default 19 Apr 2012 @ 12.28

    Hey Frinkky, Thanks for this!

    quote
    [FONT=FontinSansRegular]When one is clicked, javascript adds a class of 'active' to that li, like so:[/FONT]
    That is the bit I did not understand, a foreign language to me; entendeu cara?

    quote
    [FONT=FontinSansRegular]You'll notice that '.active.tab' has no space so it isn't matching a child '.tab' with a parent '.active', just an element that has those two classes.[/FONT]
    Learnt something here

    I could not get the code to work, so I looked at the source of the live example you provided, it showed the js like this which worked!?

    Code:
    <script type="text/javascript">
    $(window).load(function(){ // this line seemed to get it to work
    $('.tab').live('click', function() {
        $(this).addClass('active');
        $(this).siblings().removeClass('active');
    });
    });  
    </script>
    Last edited by Jacob; 19 Apr 2012 at @ 12.36.
      Reply With Quote

  3. #13
    Administrator Frinkky's Avatar
    Join Date
    Dec 2008
    Posts
    1,817
    Blog Entries
    1

    Default 19 Apr 2012 @ 15.14

    Yep, it's often necessary to dictate when the snippet is to be ready and that is done with $(window).load or $(document).ready.

    The difference between the two is that $(document).ready fires when the html is loaded and the DOM is ready, but things like images may not be loaded yet. This is where $(window).load comes in, this only triggers when everything is loaded and ready.

    In your case, the live 'click' event is probably best activated on $(document).ready so if people start clicking away before images are fully loaded it'll still work.
      Reply With Quote

  4. #14
    Member Jacob's Avatar
    Join Date
    Feb 2009
    Posts
    247

    Default 4 Aug 2012 @ 10.33

    I am now trying to get 'tab1' to be active when the page opens, I tried fiddling with your code but do not know js well enough.
      Reply With Quote

  5. #15
    Member Jacob's Avatar
    Join Date
    Feb 2009
    Posts
    247

    Default 4 Aug 2012 @ 11.18

    I found this, which works not sure where to put it, can it go on a line of it's own?

    Code:
    $("li:#tab1").addClass("active")
      Reply With Quote

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •