1

I was wondering if you could make this:

if(document.getElementsByClassName("whatever")[0]) {
    document.getElementsByClassName("whatever")[0].style.display = "none";
}

(It checkes if the element exists, and then only excicutes the code if it is true)

But without the if statment, because it doesn't look like a good practise to dublicate the document.getElementsByClassName("whatever")[0], and it makes it more anoying when having to do alot of bug testing with the document.getElementsByClassName.

Tobias H.
  • 426
  • 1
  • 6
  • 18
  • You can save off the results of `document.getElementsByClassName("whatever")` into a variable and then check it as many times as you want without having to re-look through the DOM – mhodges Mar 01 '21 at 21:21
  • Do you need it to work for all elements with the specified classNames or just first one? – Dan Cantir Mar 01 '21 at 21:21
  • A good chance to read up [optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) – Dominik Mar 01 '21 at 21:22
  • Nice one @jarmod :) – Dominik Mar 01 '21 at 21:23
  • @Dominik Just realized optional chaining is [not usable in the assignment case](https://stackoverflow.com/questions/58878948/optional-chaining-on-the-left-side-in-javascript), sadly. – jarmod Mar 01 '21 at 21:24

4 Answers4

1

Simply set it to a variable, check if anything was found, and use that variable to modify it's display:

let whatever = document.getElementsByClassName("whatever");
if(whatever.length  > 0) {
    whatever[0].style.display = "none";
}
Dave
  • 28,833
  • 23
  • 113
  • 183
1

If you need to only hide the first element with a specific className, you can define a method like this:

function hideIfExists(className){
    var elements = document.getElementsByClassName(className);
    if(elements.length  > 0){
      elements[0].style.display = 'none';
    }
   
}

Then use it like this:

hideIfExists('hello')
hideIfExists('someOtherClass')
...

If you need it to hide all elements, your method might look like this

function hideIfExists(className){
    var elements = document.getElementsByClassName(className);
    for (let item of elements) {
      item.style.display = 'none';
    }
}
Dan Cantir
  • 2,915
  • 14
  • 24
1

In my personal opinion, this is the simplest and still most elegant way to avoid the duplicate code, but also avoid extra code lines, so to keep it well readable:

if(elem = document.getElementsByClassName("whatever")[0]) {
    elem.style.display = "none";
}

Your editor might give you a warning about an assignment in a conditional expression. This seems totally okay to me, but you can avoid it by extra parens around the assignment. However, I think this reduces readability and should therefore better not be done.

not2savvy
  • 2,902
  • 3
  • 22
  • 37
0

You could destructure the element and assign only if element is truthy.

const [element] = document.getElementsByClassName("whatever");

if (element) element.style.display = "none";
<div class="whatever">1</div>
<div class="whatever">2</div>
<div class="whatever">3</div>
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392