3

I have two buttons that are essentially "next" and "previous" buttons for my tabbed form with bootstrap. The code works when you go from the introduction tab to the applicant tab just fine the way it is. The only problem is there are 11 "tabs" in the element. I am needing essential parts in this <a> to change on each tab.

What I am needing to change is the href , .style.width , and the two classNames at the end of the `onclick' function each time the user progresses through the form.

<a href="" data-toggle="tab" class="btn btn-primary pull-left hide" onclick="document.getElementById('progressBar').style.width='20%'; document.getElementById('applicantTab').className='active'; document.getElementById('introductionTab').className='';">Previous</a>

<a href="#applicant" data-toggle="tab" class="btn btn-primary pull-right" onclick="document.getElementById('progressBar').style.width='20%'; document.getElementById('applicantTab').className='active'; document.getElementById('introductionTab').className='';">Next</a>

The sections that need to correspond with the buttons are identified with an id that is different for all 11 sections.

<div class="tab-pane" id="confirmation">

So for example when the users clicks the button "next" from the introduction tab I need the above to change to: (and vise versa for the previous button)

<a href="#introduction" data-toggle="tab" class="btn btn-primary pull-left" onclick="document.getElementById('progressBar').style.width='10%'; document.getElementById('introductionTab').className='active'; document.getElementById('applicantTab').className='';">Previous</a>

<a href="#applicant" data-toggle="tab" class="btn btn-primary pull-right" onclick="document.getElementById('progressBar').style.width='20%'; document.getElementById('applicantTab').className='active'; document.getElementById('introductionTab').className='';">Next</a>

and so on throughout the tab-panes.

I am figuring I could do something like, I just really don't know how to start it and how to change just the specific onclick elements like I am needing to.

if(this tab-pane is something?) 
{
   document.getelementById('previous').href=""; 
   document.getelementById('previous').style.width="";
   document.getelementById('previous').className="";
}

As Requested in the comments the tabs are laid out like so:

<div class="tab-content"> 
  <div class="tab-pane" id="introduction"> 
    //content here 
  </div> 
  <div class="tab-pane" id="applicant">  //and so on just like this for all 11 tabs.
    //content here
  </div>
</div>
user2371301
  • 3,294
  • 4
  • 18
  • 26
  • I know that the javascript provided at the bottom of the page is not valid, I am just showing how I thought the script could possibly be laid out. it is not intended to be functional. – user2371301 Jun 12 '13 at 04:37
  • Youll need to post the structure of your tabs to get a decent answer, as the answer will likely include some sort of parent:child combination that can only be derived from the structure of your html – Kylie Jun 12 '13 at 04:40
  • Cuz it will involve reading what tab youre on, then figuring out which tab is next to it, on the left and right, and applying – Kylie Jun 12 '13 at 04:40
  • As requested I updated my answer with the tab structure. I hope that is what you are meaning :) – user2371301 Jun 12 '13 at 04:43
  • Yes, and when one is active, it has the class of 'active', yes? – Kylie Jun 12 '13 at 04:46
  • You got it! each `
    ` is changed to `
    ` when it is being viewed.
    – user2371301 Jun 12 '13 at 04:49
  • Quick question, do you have to build your own?? Or are you against using a library, cause jquery-ui has ui-tabs built into it – Kylie Jun 12 '13 at 04:54
  • Also, where you are your tab buttons, like not the tab pane, but the actual tabs. (like a
      of the tabs you can click etc)
    – Kylie Jun 12 '13 at 05:02
  • Well I have
      that act like what you are speaking of, But they are not clickable (i.e they don't have `href`) to force the users to complete the form in order using the `next` and `previous` buttons.
    – user2371301 Jun 12 '13 at 05:04
  • Are you using Jquery, or bootstrap at all? – Kylie Jun 12 '13 at 05:13
  • I can give you a solution using Jquery/Bootstrap....but if thats not what you want, then I'll just let someone else figure it out – Kylie Jun 12 '13 at 05:15
  • Yes I am using bootstrap and Jquery ALOT in this project. (basically all it is) :) – user2371301 Jun 12 '13 at 05:15
  • K, got your answer....but it involves the tabs being clickable...Ill try figure something out for that – Kylie Jun 12 '13 at 05:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31624/discussion-between-user2371301-and-kylek) – user2371301 Jun 12 '13 at 06:50

1 Answers1

3

Here you go... This is using Twitter bootstrap and Jquery... Hopefully its helpful to you... The progress bar is a bit of a hack, but its a start and Im sure you can figure things out from there...

The only thing that sucks is that the tabs are clickable, and that isn't what you wanted, since you want them to progress through the form without being able to click on the tabs....But the nuts and bolts are here

DEMO HERE

$(document).ready(function(){

  //Progress bar calculation
  function setProgress(){
    var length = $tabs.length;
    var index = ($('li').index($(".active")))+1;
    var width = (index/length)*100;
    $('#progressBar').css('width', width+'%');
  }

   var $tabs = $('#tab_bar li');
   setProgress();

   $('#prevtab').on('click', function () {
     $tabs.filter('.active').prev('li').find('a[data-toggle="tab"]').tab('show');
     setProgress();
   });

   $('#nexttab').on('click', function () {
     $tabs.filter('.active').next('li').find('a[data-toggle="tab"]').tab('show');
     setProgress();
   });

   $tabs.click(function () {
    setTimeout(setProgress, 200)
   });

});

Then your HTML, I dunno what you had for < ul > , cuz you didn't provide that part... so I just hacked something up...but this should be similar to your structure

    <ul class="nav nav-tabs" id="tab_bar">
      <li class="active"><a href="#introduction" data-toggle="tab">Intro</a></li>
      <li><a href="#secondtab" data-toggle="tab">SecondTab</a></li>
      <li><a href="#thirdtab" data-toggle="tab">Third</a></li>
      <li><a href="#fourthtab" data-toggle="tab">Fourth</a></li>
    </ul>    


 <div class="tab-content"> 

    <div class="tab-pane" id="introduction"> 
      asdasdasdas
    </div> 

    <div class="tab-pane" id="secondtab"> 
      asdasdsagreteryterythhgh
    </div> 

    <div class="tab-pane" id="thirdtab"> 
      rthrthrthrthrt
    </div> 

    <div class="tab-pane" id="fourthtab"> 
      yujghjuyjyujedgjhegj
    </div>

</div>


  <a class="btn" id="prevtab" type="button">Prev</button>
  <a class="btn" id="nexttab" type="button">Next</button>

DEMO HERE

Kylie
  • 11,421
  • 11
  • 47
  • 78
  • Yeah, Ive been trying to figure it out....Ill let you know if I do....I updated my answer above, to reflect what we just talked about, for future answer seekers :) – Kylie Jun 12 '13 at 06:35
  • Yeah the progress bar doesn't play well with clicking the `
  • `
  • – user2371301 Jun 12 '13 at 06:40
  • This might be useful to you...http://stackoverflow.com/questions/9237314/can-you-disable-tabs-in-bootstrap – Kylie Jun 12 '13 at 06:40
  • Yeah it should now though, I fixed that part...it was a bug...I threw in a setTimeout and it worked pretty good – Kylie Jun 12 '13 at 06:41
  • the answer you linked to..... It suggest you removing the `data-toggle="tab"` from the `` which is the main provider in your script. Kills the whole thing. – user2371301 Jun 12 '13 at 06:44
  • I moved this conversation to a conversation... Please continue there as we are supposed to be keep conversation on the answers page short. continue here: http://chat.stackoverflow.com/rooms/31624/discussion-between-user2371301-and-kylek – user2371301 Jun 12 '13 at 06:51