How to check the existence of a diva correctly?

window.onload = function() { // тайтлы var elemForTitle = document.getElementsByClassName("field-name-field-season"); if (elemForTitle.length > 0) { elemForTitle[0].title += "Время использования изделия: зима, лето или круглый год"; elemForTitle[0].className += " titleshowcl"; } var elemForTitleS1 = document.getElementsByClassName("field-name-field-weight"); var elemForTitleS2 = document.getElementsByClassName("field-name-field-length"); var elemForTitleS3 = document.getElementsByClassName("field-name-field-shirina"); var elemForTitleS4 = document.getElementsByClassName("field-name-field-visota"); var elemForTitleS5 = document.getElementsByClassName("field-name-field-crew"); if (elemForTitleS1.length > 0) { elemForTitleS1[0].title += " Вес изделия"; elemForTitleS1[0].className += " titleshowcl"; } if (elemForTitleS2.length > 0) { elemForTitleS2[0].title += " Длина изделия"; elemForTitleS2[0].className += " titleshowcl"; } if (elemForTitleS3.length > 0) { elemForTitleS3[0].title += " Ширина изделия"; elemForTitleS3[0].className += " titleshowcl"; } if (elemForTitleS4.length > 0) { elemForTitleS4[0].title += " Высота изделия"; elemForTitleS4[0].className += " titleshowcl"; } if (elemForTitleS5.length > 0) { elemForTitleS5[0].title += " Вместимость изделия, человек"; elemForTitleS5[0].className += " titleshowcl"; } }; 
 <div class="group-spec field-group-div"> <div class="field field-name-field-weight field-type-number-integer field-label-inline clearfix"> <div class="field-label">Вес:&nbsp;</div> <div class="field-items"> <div class="field-item even">24 кг</div> </div> </div> <div class="field field-name-field-length field-type-number-integer field-label-inline clearfix"> <div class="field-label">Длина:&nbsp;</div> <div class="field-items"> <div class="field-item even">550 см.</div> </div> </div> <div class="field field-name-field-crew field-type-range-integer field-label-inline clearfix"> <div class="field-label">Экипаж:&nbsp;</div> <div class="field-items"> <div class="field-item even">2&ndash;3 чел.</div> </div> </div> </div> 

  • one
    if (elemForTitle.length === 0) { , because getElementsByClassName always returns an array, and never - null. - Yaant
  • one
    @Yaant, strictly speaking not an array - but Live-collection - Grundy
  • one
    @Grundy, yes, I already wanted to correct my comment myself, but I did not have time. :) - Yaant

1 answer 1

getElementsByClassName returns the collection. Therefore, to check if there is something in it or not, you need to check the length property

The length property indicates how many elements are in the collection. If there are no elements in the collection, the value will be 0, if there is at least 1 - not 0.

  • changed the code elemForTitle.length === 0 - the same fairy tale (( - Igor Fostyak
  • one
    @IhorFostyak, well, so obviously, if length == 0 Then there are no elements in the collection. - Grundy
  • @ Igor Fostyak, updated the answer - Grundy
  • got it The syntax is lame in me. ! = like this or> 0 you need to put - Igor Fostyak
  • one
    @IhorFostyak I, by the way, really stumbled in my comment, it’s necessary to check the length and truth in this case not for equality to zero, but on the contrary, for inequality. :) - Yaant