Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!