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

176
Views
How to reach a html <span> tag inside a <div> using Java Script?

I've got a span tag which placed inside a div, like below:

<div onclick="doActiveCheckBox('color1')" id="sss"  class="test form-check form-option form-check-inline mb-2">
     <input class="test form-check-input" type="radio" name="color" id="color1" data-bs-label="colorOption" value="/تاریک" checked="">                                
     <label class="form-option-label rounded-circle" for="color1"><span style="border:inherit; border-block-color:purple;"  class="form-option-color rounded-circle" style="background-image: url(/img/ProductColors/green1.jpg)"></span></label>
</div>

I want to set a border for the span tag as the input tag is clicked and also don't want to use any id in the span. I've tried the bellow ways:

1:

        var d=document.getElementById("sss").getElementsByTagName("span");
        d.style.border = "thick solid #0000FF";

2:

        var d=document.getElementById("sss").getElementsByClassName();
        for (var i = 1; i <= d.length; i++) {
            d[1].style.border = "thick solid #0000FF";
        }

but none of them worked correctly! So would anybody help?

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

I would suggest to use querySelector: const d = document.querySelector("#sss span");

And then you can add style: d.style.border = "thick solid #0000FF";

about 4 years ago · Santiago Gelvez Report

0

  var d=document.getElementById("sss").getElementsByTagName("span");
                                                 ^//returns an array
d.get.style.border = "thick solid #0000FF";//d[0].style
//┗━━┛ 

There is not a property called get, so access the first element since getElementsByTagName return an array.

about 4 years ago · Santiago Gelvez Report

0

Jquery method if needed

var span = $("#sss label span");
span.css("border", "thick solid #0000FF");

or if you're not using #sss just the inputs (may get multiple results)

var span = $("input.form-check label span");
about 4 years ago · Santiago Gelvez 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!