I want to use JS to display an error message as a placeholder in the Notes <textarea> if the Broken radio button is selected and the <textarea> is empty.
I can successfully select my radio buttons via classname and use a for loop to iterate through them. I'm trying to check the associated <textarea> value in the for loop. However, whenever I check the value of the <textarea> it always comes back as undefined.
Even when I add document.querySelector("textarea").value or ...notes.value[i] == "" in the if statement, the text area value still comes back as undefined. What am I doing wrong?
I currently only have the noteError() function added to the first broken radio button.
function noteError(){
const broken = document.getElementsByClassName('broken');
var notes = document.querySelector("textarea");
for(var i = 0; i < broken.length; i++){
console.log(notes[i]);
if(broken[i].checked && notes[i] == ""){
console.log("notes is empty and broken is checked.")
}
}
}
<body>
<!-- Template Table -->
<table>
<caption>
<h1>Basic Processes</h1>
</caption>
<tr>
<th>Process</th>
<th>Status</th>
<th class="notes">Notes</th>
</tr>
<tr>
<td>Document Retrieval</td>
<td>
<input type="radio" class="working" name="retrieval" value="working">
<label for="working">Working</label>  
<input type="radio" class="broken" name="retrieval" value="broken" onclick="noteError()">
<label for="broken">Broken</label><br>
</td>
<td class="notes"><textarea></textarea></td>
</tr>
<tr>
<td>Viewing Documents</td>
<td>
<input type="radio" class="working" name="view_docs" value="working">
<label for="working">Working</label>  
<input type="radio" class="broken" name="view_docs" value="broken">
<label for="broken">Broken</label><br>
</td>
<td class="notes"><textarea></textarea></td>
</tr>
<tr>
<td>View Keywords</td>
<td>
<input type="radio" class="working" name="keywords" value="working">
<label for="working">Working</label>  
<input type="radio" class="broken" name="keywords" value="broken">
<label for="broken">Broken</label><br>
</td>
<td class="notes"><textarea></textarea></td>
</tr>
<tr>
<td>Print Documents</td>
<td>
<input type="radio" class="working" name="print" value="working">
<label for="working">Working</label>  
<input type="radio" class="broken" name="print" value="broken">
<label for="broken">Broken</label><br>
</td>
<td class="notes"><textarea></textarea></td>
</tr>
</table>
</body>