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

132
Views
Change CSS property only in getElementsByName

Looking for a way to change the CSS property by certain names.

Down below I have an example using checkboxes. There are two types that are generally the same unless changeSpecialCheckBox() occurs with a true value within its parameter.

file.css

.checkBoxApperance {
    width: 100px;
    height: 50px;
    background: 'dark-blue';
}

file.html

<!--regularCheckBox-->
<div class="checkBoxApperance">
   <input type="checkbox" id="regularCheckBox1" name="regularCheckBox"/>
</div>
<div class="checkBoxApperance">
   <input type="checkbox" id="regularCheckBox2" name="regularCheckBox"/>
</div>
<div class="checkBoxApperance">
   <input type="checkbox" id="regularCheckBox3" name="regularCheckBox"/>
</div>

<!--specialCheckBox-->
<div class="checkBoxApperance">
   <input type="checkbox" id="specialCheckBox1" name="specialCheckBox"/>
</div>
<div class="checkBoxApperance">
   <input type="checkbox" id="specialCheckBox2" name="specialCheckBox"/>
</div>
<div class="checkBoxApperance">
   <input type="checkbox" id="specialCheckBox3" name="specialCheckBox"/>
</div>

file.js

function changeSpecialCheckBox(condition)
{
  if(condition) {
    document.getElementsByName("specialCheckBox").forEach(elem => {
      elem.disabled = true;
      $('.checkBoxApperance').css("background-color" , "light-blue"); //change the appearance
                                                                     //of all checkboxes
                                                                     //instead of all checkboxes under
                                                                     //the "specialCheckBox" name
      });
  }
}

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

In this case you can simply use Node.parentNode to target the parent element of the 'specialCheckBox' elements:

function changeSpecialCheckBox(condition) {
  if (condition) {
    document.getElementsByName('specialCheckBox').forEach(elem => {
      elem.disabled = true;
      elem.parentNode.style.background = 'lightblue'
    })
  }
}

changeSpecialCheckBox(1)
.checkBoxApperance {
  width: 100px;
  height: 30px;
  background-color: darkblue;
}
<div class="checkBoxApperance">
  <input type="checkbox" id="regularCheckBox1" name="regularCheckBox" />
</div>
<div class="checkBoxApperance">
  <input type="checkbox" id="regularCheckBox2" name="regularCheckBox" />
</div>
<div class="checkBoxApperance">
  <input type="checkbox" id="regularCheckBox3" name="regularCheckBox" />
</div>

<!--specialCheckBox-->
<div class="checkBoxApperance">
  <input type="checkbox" id="specialCheckBox1" name="specialCheckBox" />
</div>
<div class="checkBoxApperance">
  <input type="checkbox" id="specialCheckBox2" name="specialCheckBox" />
</div>
<div class="checkBoxApperance">
  <input type="checkbox" id="specialCheckBox3" name="specialCheckBox" />
</div>

about 4 years ago · Juan Pablo Isaza Report

0

If you are using jQuery to set the style, then you can use $("[name='name']") to select the elements directly instead of combining jQuery with the vanilla JS getElementsByName(). This way you can chain the operations to only apply your changes to the relevant selection of elements, like this:

// You can run this inside the function/condition you want
$("[name='specialCheckBox']") // Selects the elements with the matching name
  .prop("disabled", true) // Sets the disabled property
  .closest(".checkBoxApperance") // Finds the parent div
  .css("background-color", "lightblue") // Applies the new style to the parent
.checkBoxApperance {
  width: 100px;
  height: 50px;
  background: darkblue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!--regularCheckBox-->
<div class="checkBoxApperance">
  <input type="checkbox" id="regularCheckBox1" name="regularCheckBox" />
</div>
<div class="checkBoxApperance">
  <input type="checkbox" id="regularCheckBox2" name="regularCheckBox" />
</div>
<div class="checkBoxApperance">
  <input type="checkbox" id="regularCheckBox3" name="regularCheckBox" />
</div>

<!--specialCheckBox-->
<div class="checkBoxApperance">
  <input type="checkbox" id="specialCheckBox1" name="specialCheckBox" />
</div>
<div class="checkBoxApperance">
  <input type="checkbox" id="specialCheckBox2" name="specialCheckBox" />
</div>
<div class="checkBoxApperance">
  <input type="checkbox" id="specialCheckBox3" name="specialCheckBox" />
</div>

about 4 years ago · Juan Pablo Isaza Report

0

 const checkBoxApperanceElemnts =
      document.getElementsByClassName("checkBoxApperance");
    for (var i = 0; i < checkBoxApperanceElemnts.length; i++) {
      const root = checkBoxApperanceElemnts[i];
      const el = root.querySelectorAll('[name="specialCheckBox"]');
      if (el[0]) {
        root.style.background = "red";
      }
    }
.checkBoxApperance {
    width: 100px;
    height: 50px;
    background: 'dark-blue';
}
<html>
  <bod>
    <!--regularCheckBox-->
    <div class="checkBoxApperance">
      <input type="checkbox" id="regularCheckBox1" name="regularCheckBox" />
    </div>
    <div class="checkBoxApperance">
      <input type="checkbox" id="regularCheckBox2" name="regularCheckBox" />
    </div>
    <div class="checkBoxApperance">
      <input type="checkbox" id="regularCheckBox3" name="regularCheckBox" />
    </div>
    <div class="checkBoxApperance">
      <input type="checkbox" id="specialCheckBox1" name="specialCheckBox" />
    </div>
    <div class="checkBoxApperance">
      <input type="checkbox" id="specialCheckBox2" name="specialCheckBox" />
    </div>
    <div class="checkBoxApperance">
      <input type="checkbox" id="specialCheckBox3" name="specialCheckBox" />
    </div>
  </body>
 </html>

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!