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
In the
if
statement, I assume you want to compare it with a string but the code is assigning a new value to thedrop
variable instead. You can make use of==
or===
to do a comparison.It is also a good idea to make use of
const
(orlet
) instead ofvar
so that you will probably get some IDE hints and it will be easier to debug.The issue with your if statement it should be == not =