1

I am trying to hide the visibility of this div class however javascript code that I have written didn't work.What should I do in this situation?

<div class="form-item webform-component webform-component-display webform-component--1-contact-1-bg20-custom-4">
  <label>Question? </label>
 Nein
</div>



<script>
document.getElementsByClassName("form-item webform-component webform-component-display webform-component--1-contact-1-bg20-custom-4").style.visibility = '"hidden";
</script>

1
  • 5
    It's not a class name having spaces. It's 4 different class names. Commented Oct 26, 2017 at 13:50

2 Answers 2

2

There are three things

  1. It's not a single classname having spaces, it's four different classes in a single div

  2. You have put up an extra single quote before double cotes.

  3. getElementsByCLassName returns an array so you need to specify index

Ideally, it should be

<div class="form-item webform-component webform-component-display webform-component--1-contact-1-bg20-custom-4">
<label>Question? </label>
Nein
</div>


<script>
document.getElementsByClassName("form-item")[0].style.visibility = "hidden";
</script>

Or if we write it your way

<div class="form-item webform-component webform-component-display webform-component--1-contact-1-bg20-custom-4">
<label>Question? </label>
Nein
</div>


<script>
document.getElementsByClassName("form-item webform-component webform-component-display webform-component--1-contact-1-bg20-custom-4")[0].style.visibility = "hidden";
</script>

You were missing index to be specified after getElementsByClassName and additional '

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

2 Comments

You forgot that getElementsByClassName returns an array-like object, so it should be document.getElementsByClassName("form-item")[0].style.visibility = "hidden";
@Mr.Greenwoodz yeah I was updating it . Thanks
0

document.getElementsByClassName will return you an array of elements. So if you are sure that you just have one element with that combination of classes then you can try

 document.getElementsByClassName
 ("form-item webform-component webform-component-display 
   webform-component--1-contact-1-bg20-custom-4")[0].style.visibility = "hidden";

2 Comments

The trouble is that OP believes "form-item webform-component webform-component-display webform-component--1-contact-1-bg20-custom-4" is one class name with spaces.
@Jeremy Thille, your comment on the question should get him to check back on the difference, hopefully. Thanks for that @RogerC!

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.