document.getElemetById returns TypeError: Cannot set property 'checked' of undefined












-1















I can get all radio buttons in the if statement and can manipulate them. However in the else block it throws "TypeError: Cannot set property 'checked' of undefined"



I have a form for two purposes. Add and edit. When I use the form for edit it fils the values from the state. I find the right radio button to check and check it. This part works fine. when I use the form for adding new data I update components' state by change events. Sometimes the last checked radio button stays checked and if I don't toggle it I can not get radio buttons data. Else part of the code is to clear all radio buttons checks. But it throws "TypeError: Cannot set property 'checked' of undefined" I do not know if it is because of React.
what is wrong with this code?



if (formControl.buttonText === "Düzenle") {
let correctAnswer = question.correctAnswer;
let radioButtons = document.getElementsByName("cevap");
for (let i = 0; i < 4; i++) {
if (radioButtons[i].value === correctAnswer)
radioButtons[i].checked = true;
}
}

else {
let radioButtons = document.getElementsByName("cevap"); //it throws error here
for (let i = 0; i < 4; i++) {
radioButtons[i].checked = false;
}
}


Here is the complete code:



import React from "react";

const AddQuestion = ({
formControl,
question,
handleView,
handleChange,
handleSubmit,
handleQuestionDelete
}) => {
//checks the radio button according to selected question for edit.
if (formControl.buttonText === "Düzenle") {
let correctAnswer = question.correctAnswer;
let radioButtons = document.getElementsByName("cevap");
for (let i = 0; i < 4; i++) {
radioButtons[i].checked = radioButtons[i].value === correctAnswer;
}
} else {//unchecks the radio buttons for addinh new data.
let radioButtons = document.getElementsByName("cevap");
for (let i = 0; i < 4; i++) {
radioButtons[i].checked = false;
}
}

return (
<div className="container">
<button className={formControl.addQuestionButton} onClick={handleView}>
Soru Ekle
</button>
<div className={formControl.visible}>
<form>
<div className="form-group">
<label htmlFor="Textarea1">Konu Seçiniz.</label>
<select
className="form-control"
id="topic"
onChange={handleChange}
value={question.topic}
>
<option value="">Seçiniz</option>
<option value="tarih">Tarih</option>
<option value="matematik">Matematik</option>
<option value="cografya">Coğrafya</option>
</select>
</div>
<div className="form-group inline-form">
<label htmlFor="puan">Soru Puanı Giriniz.</label>
<input
type="number"
step="5"
id="point"
className="form-control"
placeholder="Puan"
value={question.point}
onChange={handleChange}
/>
</div>
<div className="form-group">
<label htmlFor="Textarea1">Soruyu Giriniz.</label>
<textarea
className="form-control"
id="questionText"
rows="2"
placeholder="Soruyu giriniz."
value={question.questionText}
onChange={handleChange}
/>
</div>
<label className="form-control">Seçenekleri Giriniz.</label>
<div className="form-group inline-form">
<div className="inline-form">
<input
type="text"
id="answer1"
className="form-control"
placeholder="Seçenek 1"
value={question.answer1}
onChange={handleChange}
/>
</div>
<div className="inline-form">
<label className="form-check-label" htmlFor="cevap">
Doğru mu?
</label>
<input
className="form-check-input"
type="radio"
name="cevap"
id="correctAnswer"
value="answer1"
onChange={handleChange}
/>
</div>
</div>

<div className="form-group inline-form">
<div className="inline-form">
<input
type="text"
id="answer2"
className="form-control"
placeholder="Seçenek 2"
value={question.answer2}
onChange={handleChange}
/>
</div>
<div className="inline-form">
<label className="form-check-label" htmlFor="cevap">
Doğru mu?
</label>
<input
className="form-check-input"
type="radio"
name="cevap"
id="correctAnswer"
value="answer2"
onChange={handleChange}
/>
</div>
</div>

<div className="form-group inline-form">
<div className="inline-form">
<input
type="text"
id="answer3"
className="form-control"
placeholder="Seçenek 3"
value={question.answer3}
onChange={handleChange}
/>
</div>
<div className="inline-form">
<label className="form-check-label" htmlFor="cevap">
Doğru mu?
</label>
<input
className="form-check-input"
type="radio"
name="cevap"
id="correctAnswer"
value="answer3"
onChange={handleChange}
/>
</div>
</div>

<div className="form-group inline-form">
<div className="inline-form">
<input
type="text"
id="answer4"
className="form-control"
placeholder="Seçenek 4"
value={question.answer4}
onChange={handleChange}
/>
</div>
<div className="inline-form">
<label className="form-check-label" htmlFor="cevap">
Doğru mu?
</label>
<input
className="form-check-input"
type="radio"
name="cevap"
id="correctAnswer"
value="answer4"
onChange={handleChange}
/>
</div>
</div>
</form>
<div className="inline-form">
<button className="button" onClick={handleSubmit}>
{formControl.buttonText}
</button>
<button
className={formControl.deleteButton}
onClick={() => {
handleQuestionDelete(question.id);
}}
>
Sil
</button>
</div>
</div>
</div>
);
};

export default AddQuestion;









share|improve this question

























  • Please post a Minimal, Complete, and Verifiable example

    – Ele
    Nov 12 '18 at 23:56






  • 1





    I think you want for (let i = 0; i < radioButtons.length; i++)

    – Ele
    Nov 12 '18 at 23:57











  • @Ele is right, or verify before get it with if (radioButtons[i])

    – Lucas Costa
    Nov 12 '18 at 23:58











  • I have only 4 radio buttons so I did not want to get lenghth dynamicaly . In the if statement everything works as it is predicted. However in the else block it can not get elelement.

    – Erdogan Cihan
    Nov 13 '18 at 0:05






  • 2





    Programming is about doing things dynamically as much as is reasonable. This includes not "hard-coding" values such as 4. As soon as your array needs to increase or decrease in size due to some requirement you are not currently aware of, you're going to have to change code that you really shouldn' t have to

    – George Jempty
    Nov 13 '18 at 0:14
















-1















I can get all radio buttons in the if statement and can manipulate them. However in the else block it throws "TypeError: Cannot set property 'checked' of undefined"



I have a form for two purposes. Add and edit. When I use the form for edit it fils the values from the state. I find the right radio button to check and check it. This part works fine. when I use the form for adding new data I update components' state by change events. Sometimes the last checked radio button stays checked and if I don't toggle it I can not get radio buttons data. Else part of the code is to clear all radio buttons checks. But it throws "TypeError: Cannot set property 'checked' of undefined" I do not know if it is because of React.
what is wrong with this code?



if (formControl.buttonText === "Düzenle") {
let correctAnswer = question.correctAnswer;
let radioButtons = document.getElementsByName("cevap");
for (let i = 0; i < 4; i++) {
if (radioButtons[i].value === correctAnswer)
radioButtons[i].checked = true;
}
}

else {
let radioButtons = document.getElementsByName("cevap"); //it throws error here
for (let i = 0; i < 4; i++) {
radioButtons[i].checked = false;
}
}


Here is the complete code:



import React from "react";

const AddQuestion = ({
formControl,
question,
handleView,
handleChange,
handleSubmit,
handleQuestionDelete
}) => {
//checks the radio button according to selected question for edit.
if (formControl.buttonText === "Düzenle") {
let correctAnswer = question.correctAnswer;
let radioButtons = document.getElementsByName("cevap");
for (let i = 0; i < 4; i++) {
radioButtons[i].checked = radioButtons[i].value === correctAnswer;
}
} else {//unchecks the radio buttons for addinh new data.
let radioButtons = document.getElementsByName("cevap");
for (let i = 0; i < 4; i++) {
radioButtons[i].checked = false;
}
}

return (
<div className="container">
<button className={formControl.addQuestionButton} onClick={handleView}>
Soru Ekle
</button>
<div className={formControl.visible}>
<form>
<div className="form-group">
<label htmlFor="Textarea1">Konu Seçiniz.</label>
<select
className="form-control"
id="topic"
onChange={handleChange}
value={question.topic}
>
<option value="">Seçiniz</option>
<option value="tarih">Tarih</option>
<option value="matematik">Matematik</option>
<option value="cografya">Coğrafya</option>
</select>
</div>
<div className="form-group inline-form">
<label htmlFor="puan">Soru Puanı Giriniz.</label>
<input
type="number"
step="5"
id="point"
className="form-control"
placeholder="Puan"
value={question.point}
onChange={handleChange}
/>
</div>
<div className="form-group">
<label htmlFor="Textarea1">Soruyu Giriniz.</label>
<textarea
className="form-control"
id="questionText"
rows="2"
placeholder="Soruyu giriniz."
value={question.questionText}
onChange={handleChange}
/>
</div>
<label className="form-control">Seçenekleri Giriniz.</label>
<div className="form-group inline-form">
<div className="inline-form">
<input
type="text"
id="answer1"
className="form-control"
placeholder="Seçenek 1"
value={question.answer1}
onChange={handleChange}
/>
</div>
<div className="inline-form">
<label className="form-check-label" htmlFor="cevap">
Doğru mu?
</label>
<input
className="form-check-input"
type="radio"
name="cevap"
id="correctAnswer"
value="answer1"
onChange={handleChange}
/>
</div>
</div>

<div className="form-group inline-form">
<div className="inline-form">
<input
type="text"
id="answer2"
className="form-control"
placeholder="Seçenek 2"
value={question.answer2}
onChange={handleChange}
/>
</div>
<div className="inline-form">
<label className="form-check-label" htmlFor="cevap">
Doğru mu?
</label>
<input
className="form-check-input"
type="radio"
name="cevap"
id="correctAnswer"
value="answer2"
onChange={handleChange}
/>
</div>
</div>

<div className="form-group inline-form">
<div className="inline-form">
<input
type="text"
id="answer3"
className="form-control"
placeholder="Seçenek 3"
value={question.answer3}
onChange={handleChange}
/>
</div>
<div className="inline-form">
<label className="form-check-label" htmlFor="cevap">
Doğru mu?
</label>
<input
className="form-check-input"
type="radio"
name="cevap"
id="correctAnswer"
value="answer3"
onChange={handleChange}
/>
</div>
</div>

<div className="form-group inline-form">
<div className="inline-form">
<input
type="text"
id="answer4"
className="form-control"
placeholder="Seçenek 4"
value={question.answer4}
onChange={handleChange}
/>
</div>
<div className="inline-form">
<label className="form-check-label" htmlFor="cevap">
Doğru mu?
</label>
<input
className="form-check-input"
type="radio"
name="cevap"
id="correctAnswer"
value="answer4"
onChange={handleChange}
/>
</div>
</div>
</form>
<div className="inline-form">
<button className="button" onClick={handleSubmit}>
{formControl.buttonText}
</button>
<button
className={formControl.deleteButton}
onClick={() => {
handleQuestionDelete(question.id);
}}
>
Sil
</button>
</div>
</div>
</div>
);
};

export default AddQuestion;









share|improve this question

























  • Please post a Minimal, Complete, and Verifiable example

    – Ele
    Nov 12 '18 at 23:56






  • 1





    I think you want for (let i = 0; i < radioButtons.length; i++)

    – Ele
    Nov 12 '18 at 23:57











  • @Ele is right, or verify before get it with if (radioButtons[i])

    – Lucas Costa
    Nov 12 '18 at 23:58











  • I have only 4 radio buttons so I did not want to get lenghth dynamicaly . In the if statement everything works as it is predicted. However in the else block it can not get elelement.

    – Erdogan Cihan
    Nov 13 '18 at 0:05






  • 2





    Programming is about doing things dynamically as much as is reasonable. This includes not "hard-coding" values such as 4. As soon as your array needs to increase or decrease in size due to some requirement you are not currently aware of, you're going to have to change code that you really shouldn' t have to

    – George Jempty
    Nov 13 '18 at 0:14














-1












-1








-1








I can get all radio buttons in the if statement and can manipulate them. However in the else block it throws "TypeError: Cannot set property 'checked' of undefined"



I have a form for two purposes. Add and edit. When I use the form for edit it fils the values from the state. I find the right radio button to check and check it. This part works fine. when I use the form for adding new data I update components' state by change events. Sometimes the last checked radio button stays checked and if I don't toggle it I can not get radio buttons data. Else part of the code is to clear all radio buttons checks. But it throws "TypeError: Cannot set property 'checked' of undefined" I do not know if it is because of React.
what is wrong with this code?



if (formControl.buttonText === "Düzenle") {
let correctAnswer = question.correctAnswer;
let radioButtons = document.getElementsByName("cevap");
for (let i = 0; i < 4; i++) {
if (radioButtons[i].value === correctAnswer)
radioButtons[i].checked = true;
}
}

else {
let radioButtons = document.getElementsByName("cevap"); //it throws error here
for (let i = 0; i < 4; i++) {
radioButtons[i].checked = false;
}
}


Here is the complete code:



import React from "react";

const AddQuestion = ({
formControl,
question,
handleView,
handleChange,
handleSubmit,
handleQuestionDelete
}) => {
//checks the radio button according to selected question for edit.
if (formControl.buttonText === "Düzenle") {
let correctAnswer = question.correctAnswer;
let radioButtons = document.getElementsByName("cevap");
for (let i = 0; i < 4; i++) {
radioButtons[i].checked = radioButtons[i].value === correctAnswer;
}
} else {//unchecks the radio buttons for addinh new data.
let radioButtons = document.getElementsByName("cevap");
for (let i = 0; i < 4; i++) {
radioButtons[i].checked = false;
}
}

return (
<div className="container">
<button className={formControl.addQuestionButton} onClick={handleView}>
Soru Ekle
</button>
<div className={formControl.visible}>
<form>
<div className="form-group">
<label htmlFor="Textarea1">Konu Seçiniz.</label>
<select
className="form-control"
id="topic"
onChange={handleChange}
value={question.topic}
>
<option value="">Seçiniz</option>
<option value="tarih">Tarih</option>
<option value="matematik">Matematik</option>
<option value="cografya">Coğrafya</option>
</select>
</div>
<div className="form-group inline-form">
<label htmlFor="puan">Soru Puanı Giriniz.</label>
<input
type="number"
step="5"
id="point"
className="form-control"
placeholder="Puan"
value={question.point}
onChange={handleChange}
/>
</div>
<div className="form-group">
<label htmlFor="Textarea1">Soruyu Giriniz.</label>
<textarea
className="form-control"
id="questionText"
rows="2"
placeholder="Soruyu giriniz."
value={question.questionText}
onChange={handleChange}
/>
</div>
<label className="form-control">Seçenekleri Giriniz.</label>
<div className="form-group inline-form">
<div className="inline-form">
<input
type="text"
id="answer1"
className="form-control"
placeholder="Seçenek 1"
value={question.answer1}
onChange={handleChange}
/>
</div>
<div className="inline-form">
<label className="form-check-label" htmlFor="cevap">
Doğru mu?
</label>
<input
className="form-check-input"
type="radio"
name="cevap"
id="correctAnswer"
value="answer1"
onChange={handleChange}
/>
</div>
</div>

<div className="form-group inline-form">
<div className="inline-form">
<input
type="text"
id="answer2"
className="form-control"
placeholder="Seçenek 2"
value={question.answer2}
onChange={handleChange}
/>
</div>
<div className="inline-form">
<label className="form-check-label" htmlFor="cevap">
Doğru mu?
</label>
<input
className="form-check-input"
type="radio"
name="cevap"
id="correctAnswer"
value="answer2"
onChange={handleChange}
/>
</div>
</div>

<div className="form-group inline-form">
<div className="inline-form">
<input
type="text"
id="answer3"
className="form-control"
placeholder="Seçenek 3"
value={question.answer3}
onChange={handleChange}
/>
</div>
<div className="inline-form">
<label className="form-check-label" htmlFor="cevap">
Doğru mu?
</label>
<input
className="form-check-input"
type="radio"
name="cevap"
id="correctAnswer"
value="answer3"
onChange={handleChange}
/>
</div>
</div>

<div className="form-group inline-form">
<div className="inline-form">
<input
type="text"
id="answer4"
className="form-control"
placeholder="Seçenek 4"
value={question.answer4}
onChange={handleChange}
/>
</div>
<div className="inline-form">
<label className="form-check-label" htmlFor="cevap">
Doğru mu?
</label>
<input
className="form-check-input"
type="radio"
name="cevap"
id="correctAnswer"
value="answer4"
onChange={handleChange}
/>
</div>
</div>
</form>
<div className="inline-form">
<button className="button" onClick={handleSubmit}>
{formControl.buttonText}
</button>
<button
className={formControl.deleteButton}
onClick={() => {
handleQuestionDelete(question.id);
}}
>
Sil
</button>
</div>
</div>
</div>
);
};

export default AddQuestion;









share|improve this question
















I can get all radio buttons in the if statement and can manipulate them. However in the else block it throws "TypeError: Cannot set property 'checked' of undefined"



I have a form for two purposes. Add and edit. When I use the form for edit it fils the values from the state. I find the right radio button to check and check it. This part works fine. when I use the form for adding new data I update components' state by change events. Sometimes the last checked radio button stays checked and if I don't toggle it I can not get radio buttons data. Else part of the code is to clear all radio buttons checks. But it throws "TypeError: Cannot set property 'checked' of undefined" I do not know if it is because of React.
what is wrong with this code?



if (formControl.buttonText === "Düzenle") {
let correctAnswer = question.correctAnswer;
let radioButtons = document.getElementsByName("cevap");
for (let i = 0; i < 4; i++) {
if (radioButtons[i].value === correctAnswer)
radioButtons[i].checked = true;
}
}

else {
let radioButtons = document.getElementsByName("cevap"); //it throws error here
for (let i = 0; i < 4; i++) {
radioButtons[i].checked = false;
}
}


Here is the complete code:



import React from "react";

const AddQuestion = ({
formControl,
question,
handleView,
handleChange,
handleSubmit,
handleQuestionDelete
}) => {
//checks the radio button according to selected question for edit.
if (formControl.buttonText === "Düzenle") {
let correctAnswer = question.correctAnswer;
let radioButtons = document.getElementsByName("cevap");
for (let i = 0; i < 4; i++) {
radioButtons[i].checked = radioButtons[i].value === correctAnswer;
}
} else {//unchecks the radio buttons for addinh new data.
let radioButtons = document.getElementsByName("cevap");
for (let i = 0; i < 4; i++) {
radioButtons[i].checked = false;
}
}

return (
<div className="container">
<button className={formControl.addQuestionButton} onClick={handleView}>
Soru Ekle
</button>
<div className={formControl.visible}>
<form>
<div className="form-group">
<label htmlFor="Textarea1">Konu Seçiniz.</label>
<select
className="form-control"
id="topic"
onChange={handleChange}
value={question.topic}
>
<option value="">Seçiniz</option>
<option value="tarih">Tarih</option>
<option value="matematik">Matematik</option>
<option value="cografya">Coğrafya</option>
</select>
</div>
<div className="form-group inline-form">
<label htmlFor="puan">Soru Puanı Giriniz.</label>
<input
type="number"
step="5"
id="point"
className="form-control"
placeholder="Puan"
value={question.point}
onChange={handleChange}
/>
</div>
<div className="form-group">
<label htmlFor="Textarea1">Soruyu Giriniz.</label>
<textarea
className="form-control"
id="questionText"
rows="2"
placeholder="Soruyu giriniz."
value={question.questionText}
onChange={handleChange}
/>
</div>
<label className="form-control">Seçenekleri Giriniz.</label>
<div className="form-group inline-form">
<div className="inline-form">
<input
type="text"
id="answer1"
className="form-control"
placeholder="Seçenek 1"
value={question.answer1}
onChange={handleChange}
/>
</div>
<div className="inline-form">
<label className="form-check-label" htmlFor="cevap">
Doğru mu?
</label>
<input
className="form-check-input"
type="radio"
name="cevap"
id="correctAnswer"
value="answer1"
onChange={handleChange}
/>
</div>
</div>

<div className="form-group inline-form">
<div className="inline-form">
<input
type="text"
id="answer2"
className="form-control"
placeholder="Seçenek 2"
value={question.answer2}
onChange={handleChange}
/>
</div>
<div className="inline-form">
<label className="form-check-label" htmlFor="cevap">
Doğru mu?
</label>
<input
className="form-check-input"
type="radio"
name="cevap"
id="correctAnswer"
value="answer2"
onChange={handleChange}
/>
</div>
</div>

<div className="form-group inline-form">
<div className="inline-form">
<input
type="text"
id="answer3"
className="form-control"
placeholder="Seçenek 3"
value={question.answer3}
onChange={handleChange}
/>
</div>
<div className="inline-form">
<label className="form-check-label" htmlFor="cevap">
Doğru mu?
</label>
<input
className="form-check-input"
type="radio"
name="cevap"
id="correctAnswer"
value="answer3"
onChange={handleChange}
/>
</div>
</div>

<div className="form-group inline-form">
<div className="inline-form">
<input
type="text"
id="answer4"
className="form-control"
placeholder="Seçenek 4"
value={question.answer4}
onChange={handleChange}
/>
</div>
<div className="inline-form">
<label className="form-check-label" htmlFor="cevap">
Doğru mu?
</label>
<input
className="form-check-input"
type="radio"
name="cevap"
id="correctAnswer"
value="answer4"
onChange={handleChange}
/>
</div>
</div>
</form>
<div className="inline-form">
<button className="button" onClick={handleSubmit}>
{formControl.buttonText}
</button>
<button
className={formControl.deleteButton}
onClick={() => {
handleQuestionDelete(question.id);
}}
>
Sil
</button>
</div>
</div>
</div>
);
};

export default AddQuestion;






javascript if-statement dom






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 0:39







Erdogan Cihan

















asked Nov 12 '18 at 23:55









Erdogan CihanErdogan Cihan

263




263













  • Please post a Minimal, Complete, and Verifiable example

    – Ele
    Nov 12 '18 at 23:56






  • 1





    I think you want for (let i = 0; i < radioButtons.length; i++)

    – Ele
    Nov 12 '18 at 23:57











  • @Ele is right, or verify before get it with if (radioButtons[i])

    – Lucas Costa
    Nov 12 '18 at 23:58











  • I have only 4 radio buttons so I did not want to get lenghth dynamicaly . In the if statement everything works as it is predicted. However in the else block it can not get elelement.

    – Erdogan Cihan
    Nov 13 '18 at 0:05






  • 2





    Programming is about doing things dynamically as much as is reasonable. This includes not "hard-coding" values such as 4. As soon as your array needs to increase or decrease in size due to some requirement you are not currently aware of, you're going to have to change code that you really shouldn' t have to

    – George Jempty
    Nov 13 '18 at 0:14



















  • Please post a Minimal, Complete, and Verifiable example

    – Ele
    Nov 12 '18 at 23:56






  • 1





    I think you want for (let i = 0; i < radioButtons.length; i++)

    – Ele
    Nov 12 '18 at 23:57











  • @Ele is right, or verify before get it with if (radioButtons[i])

    – Lucas Costa
    Nov 12 '18 at 23:58











  • I have only 4 radio buttons so I did not want to get lenghth dynamicaly . In the if statement everything works as it is predicted. However in the else block it can not get elelement.

    – Erdogan Cihan
    Nov 13 '18 at 0:05






  • 2





    Programming is about doing things dynamically as much as is reasonable. This includes not "hard-coding" values such as 4. As soon as your array needs to increase or decrease in size due to some requirement you are not currently aware of, you're going to have to change code that you really shouldn' t have to

    – George Jempty
    Nov 13 '18 at 0:14

















Please post a Minimal, Complete, and Verifiable example

– Ele
Nov 12 '18 at 23:56





Please post a Minimal, Complete, and Verifiable example

– Ele
Nov 12 '18 at 23:56




1




1





I think you want for (let i = 0; i < radioButtons.length; i++)

– Ele
Nov 12 '18 at 23:57





I think you want for (let i = 0; i < radioButtons.length; i++)

– Ele
Nov 12 '18 at 23:57













@Ele is right, or verify before get it with if (radioButtons[i])

– Lucas Costa
Nov 12 '18 at 23:58





@Ele is right, or verify before get it with if (radioButtons[i])

– Lucas Costa
Nov 12 '18 at 23:58













I have only 4 radio buttons so I did not want to get lenghth dynamicaly . In the if statement everything works as it is predicted. However in the else block it can not get elelement.

– Erdogan Cihan
Nov 13 '18 at 0:05





I have only 4 radio buttons so I did not want to get lenghth dynamicaly . In the if statement everything works as it is predicted. However in the else block it can not get elelement.

– Erdogan Cihan
Nov 13 '18 at 0:05




2




2





Programming is about doing things dynamically as much as is reasonable. This includes not "hard-coding" values such as 4. As soon as your array needs to increase or decrease in size due to some requirement you are not currently aware of, you're going to have to change code that you really shouldn' t have to

– George Jempty
Nov 13 '18 at 0:14





Programming is about doing things dynamically as much as is reasonable. This includes not "hard-coding" values such as 4. As soon as your array needs to increase or decrease in size due to some requirement you are not currently aware of, you're going to have to change code that you really shouldn' t have to

– George Jempty
Nov 13 '18 at 0:14












3 Answers
3






active

oldest

votes


















0














The "radioButtons" is empty for some reason. When you get elements by name it does not return items. If you posted the HTML the reason was going to be guessable.



Anyway, You can check that radioButtons is not empty if you want.



let radioButtons = document.getElementsByName("cevap");
if(radioButtons.length>0){
for (let i = 0; i < radioButtons.length; i++) {
radioButtons[i].checked = false;
}
}


If you do that check it will stop throwing the error. But keep in mind that it will not set checked to false either. Not until you know why the getElementsByName returned empty data from your HTML and solve that problem.






share|improve this answer































    0














    You can simplify the logic a bit here and simply assign the checked state based on the result of a comparison between the value and the correct answer. This could be done in a single statement - but I am using a ternary equation to demonstrate the outcomes of each state.



    As stated in the comments - it is preferable to dynamically check the length of the array so that you can add or remove items from the array and the code will still work.



    if (formControl.buttonText === "Düzenle") {
    let correctAnswer = question.correctAnswer;
    let radioButtons = document.getElementsByName("cevap");
    for (let i = 0; i < radioButtons.length; i++) {
    radioButtons[i].value === correctAnswer
    ? radioButtons[i].checked = true
    : radioButtons[i].checked = false;
    }
    }





    share|improve this answer


























    • Thank you for your comment. I have a form for two purposes. Add and edit. When I use the form for edit it fils the values from the state. I find the right radio button to check and check it. This part works fine. when I use the form for adding new data I update components' state by change events. Sometimes the last checked radio button stays checked and if I don't toggle it I can not get radio buttons data. Else part of the code is to clear all radio buttons checks. But it throws "TypeError: Cannot set property 'checked' of undefined" I do not know if it is because of React.

      – Erdogan Cihan
      Nov 13 '18 at 0:20





















    0














    I changed the code as below. I used radioButtons.length as suggested and it worked. I do not know the reason but it worked.



    const radioButtons = document.getElementsByName("cevap");
    if (formControl.buttonText === "Gönder") {
    for (let i = 0; i < radioButtons.length; i++) {
    radioButtons[i].checked = false;
    }
    } else {
    let correctAnswer = question.correctAnswer;
    for (let i = 0; i < radioButtons.length; i++) {
    radioButtons[i].checked = radioButtons[i].value === correctAnswer;
    }
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271813%2fdocument-getelemetbyid-returns-typeerror-cannot-set-property-checked-of-undef%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      The "radioButtons" is empty for some reason. When you get elements by name it does not return items. If you posted the HTML the reason was going to be guessable.



      Anyway, You can check that radioButtons is not empty if you want.



      let radioButtons = document.getElementsByName("cevap");
      if(radioButtons.length>0){
      for (let i = 0; i < radioButtons.length; i++) {
      radioButtons[i].checked = false;
      }
      }


      If you do that check it will stop throwing the error. But keep in mind that it will not set checked to false either. Not until you know why the getElementsByName returned empty data from your HTML and solve that problem.






      share|improve this answer




























        0














        The "radioButtons" is empty for some reason. When you get elements by name it does not return items. If you posted the HTML the reason was going to be guessable.



        Anyway, You can check that radioButtons is not empty if you want.



        let radioButtons = document.getElementsByName("cevap");
        if(radioButtons.length>0){
        for (let i = 0; i < radioButtons.length; i++) {
        radioButtons[i].checked = false;
        }
        }


        If you do that check it will stop throwing the error. But keep in mind that it will not set checked to false either. Not until you know why the getElementsByName returned empty data from your HTML and solve that problem.






        share|improve this answer


























          0












          0








          0







          The "radioButtons" is empty for some reason. When you get elements by name it does not return items. If you posted the HTML the reason was going to be guessable.



          Anyway, You can check that radioButtons is not empty if you want.



          let radioButtons = document.getElementsByName("cevap");
          if(radioButtons.length>0){
          for (let i = 0; i < radioButtons.length; i++) {
          radioButtons[i].checked = false;
          }
          }


          If you do that check it will stop throwing the error. But keep in mind that it will not set checked to false either. Not until you know why the getElementsByName returned empty data from your HTML and solve that problem.






          share|improve this answer













          The "radioButtons" is empty for some reason. When you get elements by name it does not return items. If you posted the HTML the reason was going to be guessable.



          Anyway, You can check that radioButtons is not empty if you want.



          let radioButtons = document.getElementsByName("cevap");
          if(radioButtons.length>0){
          for (let i = 0; i < radioButtons.length; i++) {
          radioButtons[i].checked = false;
          }
          }


          If you do that check it will stop throwing the error. But keep in mind that it will not set checked to false either. Not until you know why the getElementsByName returned empty data from your HTML and solve that problem.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 0:13









          user6437700user6437700

          6919




          6919

























              0














              You can simplify the logic a bit here and simply assign the checked state based on the result of a comparison between the value and the correct answer. This could be done in a single statement - but I am using a ternary equation to demonstrate the outcomes of each state.



              As stated in the comments - it is preferable to dynamically check the length of the array so that you can add or remove items from the array and the code will still work.



              if (formControl.buttonText === "Düzenle") {
              let correctAnswer = question.correctAnswer;
              let radioButtons = document.getElementsByName("cevap");
              for (let i = 0; i < radioButtons.length; i++) {
              radioButtons[i].value === correctAnswer
              ? radioButtons[i].checked = true
              : radioButtons[i].checked = false;
              }
              }





              share|improve this answer


























              • Thank you for your comment. I have a form for two purposes. Add and edit. When I use the form for edit it fils the values from the state. I find the right radio button to check and check it. This part works fine. when I use the form for adding new data I update components' state by change events. Sometimes the last checked radio button stays checked and if I don't toggle it I can not get radio buttons data. Else part of the code is to clear all radio buttons checks. But it throws "TypeError: Cannot set property 'checked' of undefined" I do not know if it is because of React.

                – Erdogan Cihan
                Nov 13 '18 at 0:20


















              0














              You can simplify the logic a bit here and simply assign the checked state based on the result of a comparison between the value and the correct answer. This could be done in a single statement - but I am using a ternary equation to demonstrate the outcomes of each state.



              As stated in the comments - it is preferable to dynamically check the length of the array so that you can add or remove items from the array and the code will still work.



              if (formControl.buttonText === "Düzenle") {
              let correctAnswer = question.correctAnswer;
              let radioButtons = document.getElementsByName("cevap");
              for (let i = 0; i < radioButtons.length; i++) {
              radioButtons[i].value === correctAnswer
              ? radioButtons[i].checked = true
              : radioButtons[i].checked = false;
              }
              }





              share|improve this answer


























              • Thank you for your comment. I have a form for two purposes. Add and edit. When I use the form for edit it fils the values from the state. I find the right radio button to check and check it. This part works fine. when I use the form for adding new data I update components' state by change events. Sometimes the last checked radio button stays checked and if I don't toggle it I can not get radio buttons data. Else part of the code is to clear all radio buttons checks. But it throws "TypeError: Cannot set property 'checked' of undefined" I do not know if it is because of React.

                – Erdogan Cihan
                Nov 13 '18 at 0:20
















              0












              0








              0







              You can simplify the logic a bit here and simply assign the checked state based on the result of a comparison between the value and the correct answer. This could be done in a single statement - but I am using a ternary equation to demonstrate the outcomes of each state.



              As stated in the comments - it is preferable to dynamically check the length of the array so that you can add or remove items from the array and the code will still work.



              if (formControl.buttonText === "Düzenle") {
              let correctAnswer = question.correctAnswer;
              let radioButtons = document.getElementsByName("cevap");
              for (let i = 0; i < radioButtons.length; i++) {
              radioButtons[i].value === correctAnswer
              ? radioButtons[i].checked = true
              : radioButtons[i].checked = false;
              }
              }





              share|improve this answer















              You can simplify the logic a bit here and simply assign the checked state based on the result of a comparison between the value and the correct answer. This could be done in a single statement - but I am using a ternary equation to demonstrate the outcomes of each state.



              As stated in the comments - it is preferable to dynamically check the length of the array so that you can add or remove items from the array and the code will still work.



              if (formControl.buttonText === "Düzenle") {
              let correctAnswer = question.correctAnswer;
              let radioButtons = document.getElementsByName("cevap");
              for (let i = 0; i < radioButtons.length; i++) {
              radioButtons[i].value === correctAnswer
              ? radioButtons[i].checked = true
              : radioButtons[i].checked = false;
              }
              }






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 13 '18 at 0:15

























              answered Nov 13 '18 at 0:06









              gavgrifgavgrif

              8,3232717




              8,3232717













              • Thank you for your comment. I have a form for two purposes. Add and edit. When I use the form for edit it fils the values from the state. I find the right radio button to check and check it. This part works fine. when I use the form for adding new data I update components' state by change events. Sometimes the last checked radio button stays checked and if I don't toggle it I can not get radio buttons data. Else part of the code is to clear all radio buttons checks. But it throws "TypeError: Cannot set property 'checked' of undefined" I do not know if it is because of React.

                – Erdogan Cihan
                Nov 13 '18 at 0:20





















              • Thank you for your comment. I have a form for two purposes. Add and edit. When I use the form for edit it fils the values from the state. I find the right radio button to check and check it. This part works fine. when I use the form for adding new data I update components' state by change events. Sometimes the last checked radio button stays checked and if I don't toggle it I can not get radio buttons data. Else part of the code is to clear all radio buttons checks. But it throws "TypeError: Cannot set property 'checked' of undefined" I do not know if it is because of React.

                – Erdogan Cihan
                Nov 13 '18 at 0:20



















              Thank you for your comment. I have a form for two purposes. Add and edit. When I use the form for edit it fils the values from the state. I find the right radio button to check and check it. This part works fine. when I use the form for adding new data I update components' state by change events. Sometimes the last checked radio button stays checked and if I don't toggle it I can not get radio buttons data. Else part of the code is to clear all radio buttons checks. But it throws "TypeError: Cannot set property 'checked' of undefined" I do not know if it is because of React.

              – Erdogan Cihan
              Nov 13 '18 at 0:20







              Thank you for your comment. I have a form for two purposes. Add and edit. When I use the form for edit it fils the values from the state. I find the right radio button to check and check it. This part works fine. when I use the form for adding new data I update components' state by change events. Sometimes the last checked radio button stays checked and if I don't toggle it I can not get radio buttons data. Else part of the code is to clear all radio buttons checks. But it throws "TypeError: Cannot set property 'checked' of undefined" I do not know if it is because of React.

              – Erdogan Cihan
              Nov 13 '18 at 0:20













              0














              I changed the code as below. I used radioButtons.length as suggested and it worked. I do not know the reason but it worked.



              const radioButtons = document.getElementsByName("cevap");
              if (formControl.buttonText === "Gönder") {
              for (let i = 0; i < radioButtons.length; i++) {
              radioButtons[i].checked = false;
              }
              } else {
              let correctAnswer = question.correctAnswer;
              for (let i = 0; i < radioButtons.length; i++) {
              radioButtons[i].checked = radioButtons[i].value === correctAnswer;
              }
              }





              share|improve this answer




























                0














                I changed the code as below. I used radioButtons.length as suggested and it worked. I do not know the reason but it worked.



                const radioButtons = document.getElementsByName("cevap");
                if (formControl.buttonText === "Gönder") {
                for (let i = 0; i < radioButtons.length; i++) {
                radioButtons[i].checked = false;
                }
                } else {
                let correctAnswer = question.correctAnswer;
                for (let i = 0; i < radioButtons.length; i++) {
                radioButtons[i].checked = radioButtons[i].value === correctAnswer;
                }
                }





                share|improve this answer


























                  0












                  0








                  0







                  I changed the code as below. I used radioButtons.length as suggested and it worked. I do not know the reason but it worked.



                  const radioButtons = document.getElementsByName("cevap");
                  if (formControl.buttonText === "Gönder") {
                  for (let i = 0; i < radioButtons.length; i++) {
                  radioButtons[i].checked = false;
                  }
                  } else {
                  let correctAnswer = question.correctAnswer;
                  for (let i = 0; i < radioButtons.length; i++) {
                  radioButtons[i].checked = radioButtons[i].value === correctAnswer;
                  }
                  }





                  share|improve this answer













                  I changed the code as below. I used radioButtons.length as suggested and it worked. I do not know the reason but it worked.



                  const radioButtons = document.getElementsByName("cevap");
                  if (formControl.buttonText === "Gönder") {
                  for (let i = 0; i < radioButtons.length; i++) {
                  radioButtons[i].checked = false;
                  }
                  } else {
                  let correctAnswer = question.correctAnswer;
                  for (let i = 0; i < radioButtons.length; i++) {
                  radioButtons[i].checked = radioButtons[i].value === correctAnswer;
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 0:58









                  Erdogan CihanErdogan Cihan

                  263




                  263






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271813%2fdocument-getelemetbyid-returns-typeerror-cannot-set-property-checked-of-undef%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Error while running script in elastic search , gateway timeout

                      Adding quotations to stringified JSON object values