Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

202
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda