I'm trying to make the radio button checked and unchecked. I have searched quite long and found answers but when I apply to my code, it doesn't work. I can only check but unable to uncheck. This is my code
function changeValue() {
let urgent = document.querySelector("#urgent-btn");
urgent.addEventListener('click', function() {
if (urgent.checked) {
urgent.setAttribute('checked', false);
console.log(urgent.checked);
} else {
urgent.setAttribute('checked', true);
console.log(urgent.checked);
}
})
}
<form id="my-form">
<h2 id="form-header">Add New Task</h2>
<button id="cancel" onclick="cancelButton(); return false;">X</button>
<br>Name<br>
<input type="text" id="task-name" placeholder="Task Name" required /><br>
<div class="same-line-input">
<div class="in-block-input-pl">
<span id="place">Place</span><br>
<input type="text" id="task-place" />
</div>
<div class="in-block-input-dep">
<span id="department">Department</span><br>
<select id="select">
<option value=""></option>
<option value="Cleanning">Cleaning</option>
<option value="Kitchen">Kitchen</option>
<option value="Receptionist">Receptionist</option>
<option value="Beltboy">Bellboy</option>
<option value="All">All</option>
</select>
</div>
</div>
<div class="descp-form">
Description<br>
<textarea rows="10" cols="50" id="description"></textarea>
</div>
<div class="urgent-form">
<input type="radio" name="urgent" value="" id="urgent-btn" onchange="changeValue ();return false;" /> Urgent
</div>
<div class="attachment-form">
Attachment:<br><input type="file" name="fileToUpload" id="fileToUpload" accept=".jpg, .png, .jpeg" onchange="previewFile()" ;/>
<img id="output" src="#" alt="Image preview" height=70 width=60>
</div><br>
<input type="submit" id="form-submit" onclick="addTask (); return false;" />
</form>
<input type="checkbox">instead. A radio button is the wrong choice here.