Help - Search - Members - Calendar
Full Version: Fixed - Javascript
Sal's RuneScape Forum > Everything... Not RuneScape > Tech Talk > Programming & Web Development
Yippee
OK, this probably has an answer so obvious that I'll slap myself upside the head over and over because of it, but I'm too tired to think too well right now and I'm working against a deadline so I can't just get some sleep first.

Spoiler: Click to Toggle the Spoiler.
CODE
function checkGames(gamesNum)
{
  alert("function");
  var id2Get = "";
  var i = 0;

  for (i = 1; i <= (parseInt(gamesNum)); i++)
  {
    alert("first loop");
    id2Get = "h" + i;
    document.getElementById(id2Get).style.visibility="visible";
    id2Get = "a" + i;
    document.getElementById(id2Get).style.visibility="visible";
  }

  for (i = ((parseInt(gamesNum))+1); i <= 16; i++)
  {
    alert("second loop");
    id2Get = "h" + i;
    document.getElementById(id2Get).style.visibility="hidden";
    id2Get = "a" + i;
    document.getElementById(id2Get).style.visibility="hidden";
  }
}


[Close]


That's the function with the errors. The function is called with an onchange event on a textbox and is passed the value contained in the textbox. It asks for a number, so that's what will be put into the textbox. I'll eventually set up something to handle other values, but I just want a general framework at the moment. There are two sets of dropdown boxes with ids of h1 through h16 and a1 through a16 respectively.

The alert boxes are for debugging that I was attempting to do.

This function is supposed to make it so when the value in the textbox is changed the webpage will only show that many dropdown boxes. For example, if the user types in '14', the script will show 14 boxes in each set and hide the last 2.

Instead all it does is light up the 'function' alert once, then the 'first loop' alert once.

Can anyone figure out what's wrong?
Roy
So you have 16 pairs of those dropdown boxes and you want that only a specific number of them are visible? Haven't tested it but I think this would work (it just loops from 1 to 16 and sets it to visible if the index isn't more than the max amount of dropdown boxes).
Spoiler: Click to Toggle the Spoiler.
CODE
function checkGames(gamesNum)
{
  alert("function");
  var i = 0;

  for (i = 1; i <= 16; i++)
   {
      alert("the only loop");
      if (i <= parseInt(gamesNum))
      {
         document.getElementById("h" + i).style.visibility="visible";
         document.getElementById("a" + i).style.visibility="visible";
      }
      else
      {
         document.getElementById("h" + i).style.visibility="hidden";
         document.getElementById("a" + i).style.visibility="hidden";
      }
   }
}


[Close]
Yippee
See, that's what my programming usually looks like. dry.gif

Yeah, that's pretty simple and should work perfectly, but it doesn't. I pasted your code in and ran it and it does the same thing. It comes up 'function', then 'the only loop' one time, then nothing.

I wonder...Maybe Firefox doesn't support parseInt? I just downloaded Opera today, so I'll try it in that and IE8.

EDIT: Doesn't work in Opera, but works perfectly in IE8. So...what's an alternative that works for all browsers? Or maybe it's the document.getElementById("a" + i).style.visibility="hidden"; that's the problem... It has to be one of those two, though. I'll see if strategically placing some more alert boxes can help out a bit there.

EDIT 2: Ah, the PHP code in that page wasn't giving the a# set their ids properly. It all works great now. Thanks for your help, Roy. smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.