-4

So I have been following a youtube tutorial and my program didn't function right

this is my javascript code it fails at if statement

document.getElementById("chec").onclick = function(){

    const visabtn = document.getElementById("visabtn");
    const mastercardbtn = document.getElementById("mastercardbtn");
    const revolutbtn = document.getElementById("revolutbtn");

    if(visabtn.checked){
        console.log("You are paying with a visa")
    }

it says there is a problem with .checked but in the tutorial he did this way

}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>

    <label for="visaBtn">Visa</label>
    <input type="radio" name="card" id="visaBtn">
    <label for="mastercardBtn">Mastercard</label>
    <input type="radio" name="card" id="mastercardBtn">
    <label for="revolutBtn">Revolut</label>
    <input type="radio" name="card" id="revolutbtn"><br>

    <button id="chec">submit</button>

    <script src="index.js"></script>
</body>
</html>
  • 1
    "*[It] didn't function right*" - what was it supposed to do? – David Thomas Jun 02 '22 at 19:03
  • 1
    What exactly is the error message? – CertainPerformance Jun 02 '22 at 19:03
  • 2
    The id "visaBtn" is not the same as the id "visabtn". – Pointy Jun 02 '22 at 19:04
  • Welcome to Stack Overflow! In what way is your code not working as expected? Please elaborate on the specific problem you are observing and what debugging you have done. To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Jun 02 '22 at 19:04
  • 1
    You should always post what the error message is. – epascarello Jun 02 '22 at 19:15
  • 1
    Like Pointy mentioned. When you need to get element by it's id. It's important that you use the id exactly as it is in your element. You wrote `const visabtn = document.getElementById("visabtn");` But the id of your input radio is `visaBtn`. One small difference and it's all wrong – Chris G Jun 02 '22 at 19:20
  • Be sure to write a title that helps distinguish your question from other similar questions on this site. – Wyck Jun 02 '22 at 19:37

0 Answers0