Skip to content
Advertisement

How to reach a html tag inside a 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?

Advertisement

Answer

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

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

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement