2

My tabs are created using PHP so I do not want a set width per tabswitcher tab. Due to this I am trying to add up the total width of the tabswitcher so I can set the width then apply a margin:0 auto;.

Alerting the var width is returning NaN. This is my first loop attempt, probably screwed up somewhere! (obviously...)

JQuery

var width;
var NumOfTabs = $('.TabSwitcher .Tab').length;
var count = 1;
while (count < NumOfTabs){
    width = width + $('.TabSwitcher .Tab:nth-child(' + count + ')').width();
    count++;
}
width = width + (NumOfTabs * 4) // Add passing
$('.TabSwitcher').css({'width': width + 'px'});

HTML

<div class="TabSwitcher">
    <div class="Tab Active" id="Page_1">1</div>
    <div class="Tab" id="Page_2">2</div>
</div>
17
  • 1
    I dont see any var margin, is it var width instead? Commented Jan 19, 2015 at 19:09
  • 3
    width is initially NULL. Hard to add NULL. Commented Jan 19, 2015 at 19:09
  • 2
    @Tomanow It is not null, it is undefined. Commented Jan 19, 2015 at 19:12
  • 2
    Are you going to argue about undefined/null and other things? Try to answer. Commented Jan 19, 2015 at 19:14
  • 2
    @AdrianIftode I see no arguing going on here. I even see an answer, twice. Commented Jan 19, 2015 at 19:19

3 Answers 3

2

I think you should consider using jQuery's handy each method, like so:

var width = 0;
var tabs = $('.TabSwitcher .Tab');
tabs.each(function(index, tab) {
    width += $(tab).width();
});
width += (tabs.length * 4); // Add padding
$('.TabSwitcher').css({'width': width + 'px'});

You should also note that width MUST be instantiated to 0. Otherwise, adding to it will only produce NaN.

Sign up to request clarification or add additional context in comments.

2 Comments

By the way, though this is a nice suggestion, buried in your answer is the fact that the only bug the OP had was that they didn't initialize width to 0
@Regent Thanks! I skimmed right over that, but it's nicer with the +=. I've updated my answer.
2

Do not keep looking things up. All that will do is make it slow since you keep looking up the same thing, over and over.

var width = 0;  //you original issue, initialize it with a start value
var tabs = $('.TabSwitcher .Tab'); //store it do not look it up over and over
var numOfTabs = tabs.length;

tabs.each( function() {  //use each to loop
    width = width + $(this).width();  //"this" is the current tab
});

width = width + (numOfTabs * 4) // Add padding
$('.TabSwitcher').css({'width': width + 'px'});

Comments

0

http://jsfiddle.net/drk01j1L/

var width = 0;

$('.TabSwitcher .Tab').each(
function(index, el) {
   width = width + $(el).width();
}
);

$('.answer').html(width);

here, you use each to find all width add them together.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.