Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
Styling through JavaScript only works sometimes

Im sure there is some underlying logic here i do not understand but cannot find a solution online. Sometimes the styles are applied, sometimes they are not despite no changes being made. When i comment out a style for one ID, another ID starts working yet they are completely independent of each other, and switching ones position in the Javascript document flow makes anothers work? Only afew days into Javascript and this is very very strange to me. I am using live server on VS code.

const result = document.querySelector("#myElement");
result.style.color = "blue";
result.style.backgroundColor = "black";
result.style.fontSize = "80px";
let button = document.getElementById("button");
button.style.color = "red";
let para = document.getElementById("p");
para.style.backgroundColor = "black";
para.style.color = "green";

here is HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="index.css" type="text/css" />

    <title>Document</title>
  </head>
  <body>
    <h1 id="myElement">this is a heading</h1>
    <p id="p">this is a pragraph, thanks</p>
    <div class="container">
      <div class="items-1 item">item 1</div>
      <div class="items-2 item">item 2</div>
      <div class="items-3 item">item 3</div>
      <div class="items-4 item">item 4</div>
      <div class="items-5 item">item 5</div>
    </div>
    <button id="btn">click me</button>
    <script src="index.js"></script>
  </body>
</html>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to call elements by their ID, tags by their TagName, etc. see below comments and corrected code.

const result = document.getElementById("#myElement"); // you get an element by it's ID //
result.style.color = "blue";
result.style.backgroundColor = "black";
result.style.fontSize = "80px";
let button = document.getElementByTagName("button"); // you get a tag by it's TagName //
button.style.color = "red";
let para = document.getElementByTagName("p"); // you get a tag by it's TagName //
para.style.backgroundColor = "black";
para.style.color = "green";
about 4 years ago · Juan Pablo Isaza Relatório

0

First, you should know that one ID should be occupied only by one HTML element. It's unique, otherwise it would be messed when you do the getElementById. If you want to refer to a specific element, you can use querySelector for single element and querySelectorAll for multiple element by using the respective selectors:

  1. # for referring an element by Id.
  2. . for referring an element by Class.
  3. Just straight by the name for referring an element by the HTML tag.

For example:

// Set all divs to have 300x150 and the border
document.querySelectorAll('div').forEach(e => {
  e.style.width = '300px';
  e.style.height = '150px';
  e.style.border = '1px solid #000';
});

// Set style specifically for one element
document.querySelector('#div_1').style.background = '#e8e8e8';

// Making all elements with class elem to have font family: serif
document.querySelectorAll('.elem').forEach(e => {
  e.style.fontFamily = 'serif';
});
<p class="elem">Lorem Ipsum</p>
<div class="elem" id="div_1">This is div 1</div>
<div class="elem" id="div_2">This is div 2</div>

P.S. Just pay attention that the querySelectorAll shouldn't be used for referring multiple elements.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda