Run css while loading Javascript CSS page

0

I'm developing a web application and would like to take the border off loading page, but it has the following error.

Uncaught TypeError: Cannot set property 'border' of undefined

JS

 <script>

    $(document).ready(function(){

   document.getElementsByClassName("linha").style.border= "0 none";
 });
</script>

CSS

.linha {
border: 1px solid #E0E0DA;
height: 390px;
}

HTML

 <div class="linha">

                <img src="imagens/camisa_1.JPG" alt="camisa1" class="imagem_teste">


    <p class="descricao_produto">Calça Jeans Armani</p>

<h4 class="preco"> A partir de R$134,99</h4>
     <button class="saiba_mais" id="saiba_mais1">SAIBA MAIS</button> 

        </div>
    
asked by anonymous 23.10.2017 / 02:03

2 answers

0

As you are already using jQuery you set the border like this:

$('.linha').css('border', '0 none');
    
23.10.2017 / 02:11
2

The getElementsByClassName function returns a list of elements that have the given class, not just 1 . You can try to access the first one by using [0] :

document.getElementsByClassName("linha")[0].style.border= "0 none";
//---------------------------------------^ aqui

Or if you have multiple use for :

for (let linhaHtml of document.getElementsByClassName("linha")){
    linhaHtml.style.border= "0 none";
}

But if you're already using JQuery, simplify and do:

$(".linha").css("border","0 none");

That works for 1 or more elements, without having to differ in code.

Example:

$(document).ready(function() {
  $(".linha").css("border","0 none");
});
.linha {
  border: 1px solid #E0E0DA;
  height: 390px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="linha">

  <img src="imagens/camisa_1.JPG" alt="camisa1" class="imagem_teste">


  <p class="descricao_produto">Calça Jeans Armani</p>

  <h4 class="preco"> A partir de R$134,99</h4>
  <button class="saiba_mais" id="saiba_mais1">SAIBA MAIS</button>

</div>

Documentation for the function getElementsByClassName

    
23.10.2017 / 02:10