HTML:
<div class="radio" id="uniform-L1"><span><input type="radio" name="length" class="radio" value="L1" id="L1"></span></div>
JS:
<script>
if(document.getElementById('L1').checked) {
console.log("chwer");
}
</script>
I tried to find what's wrong with my JS but I couldn't. I have no console errors.
Listen for the change event if you want to log "chwer" everytime the radio button is checked:
document.getElementById('L1').addEventListener('change', function() {
if (this.checked) {
console.log("chwer");
}
})
<div class="radio" id="uniform-L1"><span><input type="radio" name="length" class="radio" value="L1" id="L1"></span></div>
Select the node with querySelector
function checkSelection() {
const input = document.querySelector('input[name="length"]');
if (input.checked) {
console.log(input.value);
}
}
<div class="radio" id="uniform-L1">
<input type="radio" name="length" class="radio" value="L1" id="L1"> Radio
</div>
<button onclick="checkSelection()">Check</button>
I believe the issue is that when your run your JS that would only check to see if the radio button is checked when it loads(which wouldn't be checked). If you want the JS to activate when the radio button changed you'll have to activate an onclick to check when your button has changed and then run the script.