0

Hi I am trying to create a while loop and I am having trouble with what to put in it so far I have:

function showSports(obj)
{
    var groupId = obj.id.substring(0, 1);
    var indx = obj.id.substring(obj.id.indexOf('_') + 1);
    var id = indx.substring(0, indx.length + 1);
    var displayInfo = false;
    while (displayInfo)
    {
        if (indx == 1)
        {
            show('footballInfo');
            hide('soccerInfo');
            hide('baseballInfo');
        }
        if (indx == 2)
        {
            show('soccerdInfo');
            hide('baseballInfo');
            hide('footballInfo');
        }
        if (indx == 3)
        {
            show('baseballInfo');
            hide('footballInfo');
            hide('soccerdInfo');
        }
        displayInfo = true;
    }
}

It is supposed to be able to loop through the links below and show/hide depending on which link is selected.

<a id='1link_1a' title="football Tab" onclick='showSports(this);'>
  <span>FootBall</span>
</a>
<a id='1link_1b' title="soccer"
onclick='showSports(this); changeTab(this);'>
  <span>Soccer</span>
</a>
<a id='1link_1c' title="baseball" onclick='showSports(this);'>
  <span>Baseball</span>
</a>
3
  • 3
    Could you describe what exactly "isn't working?" Commented Feb 7, 2011 at 20:40
  • you have while (displayInfoTab) but dont declare that variable. Commented Feb 7, 2011 at 20:43
  • displayInfoTab should be displayInfo Commented Feb 7, 2011 at 21:02

2 Answers 2

1

I don't understand your use of the while statement. Perhaps you're thinking of a switch statement.

function showSports(obj)
{
    var groupId = obj.id.substring(0, 1);
    var indx = obj.id.substring(obj.id.indexOf('_') + 1);
    var id = indx.substring(0, indx.length + 1);

    switch (indx)
    {
        case 1:
            show('footballInfo');
            hide('soccerInfo');
            hide('baseballInfo');
        break;
        case 2:
            show('soccerdInfo');
            hide('baseballInfo');
            hide('footballInfo');
        break;
        case 3:
            show('baseballInfo');
            hide('footballInfo');
            hide('soccerdInfo');
        break;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Looks to me like a confusion with the rare and bad with() keyword.
0

RightSaidFred is correct, it sounds like you meant to use switch. One other point is that the line:

var id = indx.substring(0, indx.length + 1);

will have an index out of bounds error. I think you were thinking to do:

var id = indx.substring(0, indx.length - 1);

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.