I need help displaying the form. I have a divider in my body with the id="anketler"
. I want to put my form there.
<script>
class soru {
constructor(ques, opt1, opt2, opt3, opt4) {
this.ques = ques;
this.opt1 = opt1;
this.opt2 = opt2;
this.opt3 = opt3;
this.opt4 = opt4;
this.opts = [opt1, opt2, opt3, opt4];
}
}
function createForm(x) {
let form = document.createElement('form');
for (let i = 0; i <= 3; i++) {
let input = document.createElement('input');
input.type = 'radio';
input.id = x.opts[i];
input.name = x.opts[i];
input.value = x.opts[i];
form.appendChild(input);
let label = document.createElement('label');
label.for = x.opts[i];
label.innerText = x.opts[i];
form.appendChild(label);
}
let submit = document.createElement('button');
submit.type = submit;
submit.value = Gönder;
form.appendChild(submit);
document.getElementById('anketler').appendChild(form);
}
const yeni_soru = new soru(
'Soru1: En salak bekarlar diyarı üyesi ?',
'Egemen',
'Atakan',
'Batuhan',
'Kerem'
);
createForm(yeni_soru);
</script>
2
Answers
Life would be a lot easier had you given your form a name!
Then one could just…
But under the circumstances I suppose we can use…
In the code you provided, problem lies in the following lines:
submit.type
you have to assign a String:'submit'
.submit.value
. But due tosubmit
being a button, for the ‘Gönder’ text to appear as the button’s label, you have to assign the string tosubmit.innerText
instead.Here´s the result: