How to apply an event listener to multiple elements sharing ID, but only firing on the parent of the clicked element

I’m trying to develop an inspection form where a ‘corrective action’ prompt is fired when the ‘X’ radio is selected rather than the ‘checkmark’ radio.

I have used event listeners on both buttons to fire the prompt when necessary.

What i’m trying to accomplish is to use this function over multiple DIV’s rather than write specific event listeners for each section (this would result in a ton of code as I intend to have a lot of points on the inspection form). I’m hoping there is a way to share the ID’s for all the sections, and attach the event listeners to ALL ID’s, but only fire the ‘corrective action’ prompt within the parent DIV the radio was selected. (I hope this makes sense).

note the inputs for the Registration Line share the same ID as the inputs in the Insurance Line


                                                **<!--Registration Line-->**
          <div class="accordion-body vehicleBody border border-light-subtle">
                                                
            <h3 id="registration" class="mx-3">Current / Valid Vehicle Registration
                <span>
                    <input type="radio" class="btn-check reg" name="registration" id="success" autocomplete="off" >
                    <label class="btn btn-outline-success rounded-circle" for="success"><i class="fa-solid fa-check"></i></label>

                    <input  type="radio" class="btn-check" name="registration" id="danger" autocomplete="off" />
                    <label class="btn btn-outline-danger rounded-circle ms-3" for="danger"><i class="fa-solid fa-x"></i></label>
                </span>
            </h3> 
            <div class="ms-3" id="correctiveAction">
                <p class="text-center" style="font-weight: 400; padding: 10px; color: crimson; text-decoration: underline;">Corrective Action Required!</p>

                <div class="form-floating correctiveAction" >
                    <textarea class="form-control" id="floatingTextarea2" style="height: 100px"></textarea>
                    <label for="floatingTextarea2">Please determine appropriate corrective action.</label>
                  </div>
                  <div class="input-group mt-2 ">
                    <span class="input-group-text"><i class="bi bi-person-fill"></i></span>
                    <span class="input-group-text infoHead" style="width:174px;">Responsible Person:</span>
                      <input type="text" class="form-control" aria-label="employee name" aria-describedby="employee name">
                    </div>
            </div>
            </div>

                                                <!--Insurance Line-->
        <div class="accordion-body vehicleBody border border-light-subtle">
                               
            <h3 id="insurance" class="mx-3">Current / Valid Insurance
                <span>
                    <input type="radio" class="btn-check" name="insurance" id="success" autocomplete="off">
                    <label class="btn btn-outline-success rounded-circle" for="success"><i class="fa-solid fa-check"></i></label>

                    <input type="radio" class="btn-check" name="insurance" id="danger" autocomplete="off">
                    <label class="btn btn-outline-danger rounded-circle ms-3" for="danger"><i class="fa-solid fa-x"></i></label>
                </span>
             </h3> 
             <div class="ms-3" id="correctiveAction">
                <p class="text-center" style="font-weight: 400; padding: 10px; color: crimson; text-decoration: underline;">Corrective Action Required!</p>

                <div class="form-floating correctiveAction" >
                    <textarea class="form-control" id="floatingTextarea2" style="height: 100px"></textarea>
                    <label for="floatingTextarea2">Please determine appropriate corrective action.</label>
                  </div>
                  <div class="input-group mt-2 ">
                    <span class="input-group-text"><i class="bi bi-person-fill"></i></span>
                    <span class="input-group-text infoHead" style="width:174px;">Responsible Person:</span>
                      <input type="text" class="form-control" aria-label="employee name" aria-describedby="employee name">
                    </div>
            </div>
        </div>  

JS

const ca = document.getElementById("success");
const nc = document.getElementById("danger")
var ca1 = document.getElementById("correctiveAction");
nc.addEventListener("click", function(){
   ca1.style.display="block";
})
ca.addEventListener("click", function(){
    ca1.style.display="none"
})

I’ve attempted to find ways to target the input and to fire within the parent div only but i’m not finding a way to do so.

Using the JS i have included only fires the ‘corrective action’ on the Registration div even when the ‘X’ is clicked for the Insurance div.

New contributor

tDeanKR is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật