FrontEND/JavaScript

checkbox, radio박스 체크됐는지 확인

smartlittlepuppy 2024. 1. 24. 18:43
반응형
<body>
    <input type="checkbox" id="myCheckBox">
    <label for="myCheckBox">subscribe</label><br>

    <input type="radio" id="visaBtn" name="card">
    <label for="visaBtn">Visa</label><br>

    <input type="radio" id="masterCardBtn" name="card">
    <label for="masterCardBtn">masterCard</label><br>


    <button id="mySubmit" type="submit">submit</button>

    <p id="subResult"></p>
    <p id="paymentResult"></p>

    <script src="index.js"></script>

 

 

const myCheckBox = document.getElementById("myCheckBox");
const visaBtn = document.getElementById("visaBtn");
const masterCardBtn = document.getElementById("masterCardBtn");
const mySubmit = document.getElementById("mySubmit");
const subResult = document.getElementById("subResult");
const paymentResult = document.getElementById("paymentResult");

mySubmit.onclick = function(){
    if(myCheckBox.checked){
        subResult.textContent = "You are subscribed";
        if(visaBtn.checked){
            paymentResult.textContent="You are paying with Visa";
        }
        else if(masterCardBtn.checked){
            paymentResult.textContent="You are paying with masterCard";
        }
        else {
            paymentResult.textContent="You have to select payment type";
        }

    }
    else {
        subResult.textContent = "You are NOT subscribed";
        paymentResult.textContent="";
       
    }


   
}

 

myCheckBox.checked : myCheckBox가 체크됐다면,,  

반응형