user1015711
user1015711

Reputation: 132

For loop is being ignored

Why is the xyz loop getting ignored? Here is a fiddle http://jsfiddle.net/B99CD/ Please keep in mind this is inside of a proprietary content management system I cant mention.
The fiddle does not run the function for some reason. what happens is the tabs do not get set back to none and therefore all look active. Any suggestions?

Edit the fieldsets exist. I cant share that portion of the code. Sorry.

testing in ie10 and ff26.0

function tabAction(tab) {

    var numberOfTabs = document.getElementsByTagName("li");
    var x = 0;
    var xyz = 0;
    //alert(tab);
    debugger;
    var loopEnd = document.getElementsByTagName("fieldset").length;

    document.getElementsByTagName("fieldset")[tab].style.display = "block";
    document.getElementsByTagName("fieldset")[tab].style.clear = "both";
    document.getElementsByTagName("li")[tab].className = "active";

    if(tab > 5) {
        document.getElementsByTagName("li")[tab].className = "active"; // show task tab
        for(x = 6; x < loopEnd; x++) {
            document.getElementsByTagName("fieldset")[x].style.display = "block";
            document.getElementsByTagName("fieldset")[x].style.clear = "both";
        }
    }

    for(xyz = 0; xyz < numberOfTabs; xyz++){
        alert(xyz);
        if (xyz !== tab) {
            document.getElementsByTagName("li")[x].className = "none";
        }     
    }

    for(x = 0; x < loopEnd; x++)  {
        if (x !== tab) {
            document.getElementsByTagName("fieldset")[x].style.display = "none";
        }
        if (x > 5){
            document.getElementsByTagName("fieldset")[x].style.display = "none";// change this
        }
    }
} 

Upvotes: 0

Views: 975

Answers (3)

TwilightSun
TwilightSun

Reputation: 2335

I guess numberOfTabs should be document.getElementsByTagName("li").length.

Besides the numberOfTabs, shouldn't the x in document.getElementsByTagName("li")[x].className = "none"; be xyz.

And I don't think its a good idea to use document.getElementsByTagName("li") to get all the tabs, as you'll get error when there are other <li> tags in the HTML document. I suggest you wrap your tabs with an element and setting it's id, and you can use document.getElementById('xxxx').getElementsByTagName("li")

Upvotes: 2

Barmar
Barmar

Reputation: 780798

I get an error at this line:

document.getElementsByTagName("fieldset")[tab].style.display = "block";

There are no fieldset elements in your HTML, so document.getElementsByTagName("fieldset")[tab] is undefined, and you can't set properties of undefined.

Upvotes: 0

Halcyon
Halcyon

Reputation: 57709

numberOfTabs is never set.

I can't derive from your code what the value should be. Looking at your HTML I would guess 7.

Upvotes: 3

Related Questions