EQUAL-OPPORTUNITY ANNOYANCE (telophase) wrote,
EQUAL-OPPORTUNITY ANNOYANCE
telophase

Question for anyone who knows Javascript...

This isn't about a broken script, as I've been able to get it to work. It's about a weird (to me) occurrence in an incrementing variable.

ETA: Solved! It's counting all the divs in the page. Headdesk time!


function showonlyone(thechosenone) {
      var Tab = document.getElementsByTagName("div");
      for(var x=0; x<Tab.length; x++) {
      	name = Tab[x].getAttribute("name");
      	if (name == 'Tab') {
        	if (Tab[x].id == thechosenone) {
				Tab[x].style.display = 'block';
				//alert('Boo - Tab' + x);
            } else {
            	Tab[x].style.display = 'none';
				//alert('Wah - Tab' + x);
            }
        }
      }
}


See, the variable x above? It iterates through three divs, so that I can switch between them and change the display status when someone chooses an option from a drop-down menu. (Er, no comments about whether the drop-down is a good idea or not: it was told to me to do. :D)

Anyway, x is showing up as 10, 11, and 12, instead of 0, 1, and 2 as I'd expect. I ended up having to name the divs Tab10, Tab11, and Tab12 to make it work. So while it's not a serious problem, it is a perplexing one.

Ideas? Explanations?

You can comment here or at the Dreamwidth crosspost. comment count unavailable comments at Dreamwidth.
Tags: javascript can bite mello's fabulous ass
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 4 comments