0

I want to set up a page where the visibility of certain elements on a page are toggled by buttons, but for some reason, my code is simply ignored / does absolutely nothing.

I can get this to work when using ID's, but then only the first instance of the ID on the page gets changed while the rest are ignored. With classes, nothing happens.

I'm sure I'm missing something basic here, but I can't figure out what. Please check below:

function georgianInfo() {
  document.getElementByClassName("georgian").style.display = "block";
  document.getElementByClassName("international").style.display = "none";
}

function internationalInfo() {
  document.getElementByClassName("georgian").style.display = "none";
  document.getElementByClassName("international").style.display = "block";
}
.georgian {
  display: none;
}

.international {
  display: block;
}
<form><input type="button" onClick="georgianInfo()" value="georgian students"></form>

<form><input type="button" onClick="internationalInfo()" value="international students"></form>

<h4 class="margin-rmv">Admissions Information</h4>

<h5 class="georgian">Georgian Students Admissions Information</h5>

<h5 class="international">International Students Admissions Information</h5>

Refer codepen: https://codepen.io/escapetomars/pen/vxBLgg

4 Answers 4

3

Change the spelling of document.getElementsByClassName.

Also, document.getElementsByClassName returns an array/list of elements. So you have to refer it with the correct index.

Refer code:

function georgianInfo() {
  document.getElementsByClassName("georgian")[0].style.display = "block";
  document.getElementsByClassName("international")[0].style.display = "none";
}

function internationalInfo() {
  document.getElementsByClassName("georgian")[0].style.display = "none";
  document.getElementsByClassName("international")[0].style.display = "block";
}
.georgian {
  display: none;
}

.international {
  display: block;
}
<table class="tab-menu" cellspacing="0" cellpadding="0">
  <tr>
    <td>
      <form><input type="button" onClick="georgianInfo()" value="for georgian students"></form>
    </td>
    <td>
      <form><input type="button" onClick="internationalInfo()" value="for international students"></form>
    </td>
  </tr>
</table>

<h4 class="margin-rmv">Admissions Information</h4>

<h5 class="georgian">Georgian Students Admissions Information</h5>

<h5 class="international">International Students Admissions Information</h5>

I would suggest you to go through the MDN article regarding Document.getElementsByClassName for a better understanding of the construct.

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

1 Comment

Thanks so much! That fixed it, and gives me a better understanding of how this works.
0

Change functions as:

    function georgianInfo() {
      document.getElementsByClassName("georgian")[0].style.display = "block";
      document.getElementsByClassName("international")[0].style.display = "none";
    }

    function internationalInfo() {
      document.getElementsByClassName("georgian")[0].style.display = "none";
      document.getElementsByClassName("international")[0].style.display = "block";
    }

Comments

0

Correct method is document.getElementsByClassName and as the name suggests it returns an array of items. So if you have to apply anything on these elements you need to iterate over that array and then apply something on each element or you can pick a particular element using array indexes.

var element = document.getElementsByClassName('class')[0]; // assuming you have at least one matching element.
element.style.display = 'none';

Here is the link to updated codepen

http://codepen.io/vibhanshu/pen/EWYNMb

Comments

0

There is a very important difference between getElementById and getElementsByClassName. Id is unique and there is only one element by id="abc". So getElementById('abc') gets the element and you can change its styles. Some browsers if you have repeated id, will get you the first one.

But getElementsByClassName is different. There could be multiple elements class="abc". So getElementsByClassName gives you a collection, and you can not change the style of a collection by the way you did. And as mentioned in another answers you need to select an element from the collection to change its styles.

document.getElementsByClassName("georgian")[0].style.display = "block";

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.