skip to Main Content
function button() {
  var drop = document.getElementById("drop").value;
  console.log(drop);

  if (drop = "radio") {
    const para = document.createElement("button");
    para.innerHTML = "button";
    document.body.appendChild(para);
  } else if (drop = "button") {


  } else {

  }

};
<!DOCTYPE html>
<html lang="en">

<head>
  <title>hi</title>
</head>

<body>
  <h1>Jayesh forms</h1>
  <select id="drop">
    <option>radio</option>
    <option>checkbox</option>
    <option>button</option>
  </select>
  <button id="button" onclick="button()">+</button>
  <p>1</p>
  <script src="js.js"></script>
</body>

</html>

I am making a replica of google forms where a checkbox will create a new element.
the drop variable is returning different answers what got wrong?

2

Answers


  1. In the if statement, I assume you want to compare it with a string but the code is assigning a new value to the drop variable instead. You can make use of == or === to do a comparison.

    if (drop === "radio") { 
      // rest of the code
    }
    

    It is also a good idea to make use of const (or let) instead of var so that you will probably get some IDE hints and it will be easier to debug.

    Login or Signup to reply.
  2. The issue with your if statement it should be == not =

    function button() {
        var drop = document.getElementById("drop").value;
        console.log(drop);
    
        if (drop == "radio") {
            const para = document.createElement("button");
            para.innerHTML = "button";
            document.body.appendChild(para);
        } else if (drop == "button") {
    
    
        } else {
    
        }
    
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search