Answer the question
In order to leave comments, you need to log in
How to select the first matching input on page load?
We have the following code
<div class="group-options">
<div class="group-options__item">
<input type="radio" name="group-1">
<input type="radio" name="group-1" data-prop="prop1">
<input type="radio" name="group-1" data-prop="prop1">
</div>
<div class="group-options__item">
<input type="radio" name="group-2" data-prop="prop2">
<input type="radio" name="group-2" data-prop="prop2">
<input type="radio" name="group-2" data-prop="prop2">
</div>
</div>
<div class="group-options">
<div class="group-options__item">
<input type="radio" name="group-3">
<input type="radio" name="group-3">
</div>
<div class="group-options__item">
<input type="radio" name="group-4">
<input type="radio" name="group-4" data-prop="prop4">
<input type="radio" name="group-4" data-prop="prop4">
</div>
</div>
<div class="group-options__item">
<input type="radio" name="group-5">
<input type="radio" name="group-5" data-prop="prop5">
<input type="radio" name="group-5" data-prop="prop5">
</div>
<div class="group-options__item">
<input type="radio" name="group-6" data-prop="prop6">
<input type="radio" name="group-6" data-prop="prop6">
</div>
<div class="group-options">
<div class="group-options__item">
<input type="radio" name="group-1">
<input type="radio" name="group-1" data-prop="prop1" checked>
<input type="radio" name="group-1" data-prop="prop1">
</div>
<div class="group-options__item">
<input type="radio" name="group-2" data-prop="prop2">
<input type="radio" name="group-2" data-prop="prop2">
<input type="radio" name="group-2" data-prop="prop2">
</div>
</div>
<div class="group-options">
<div class="group-options__item">
<input type="radio" name="group-3">
<input type="radio" name="group-3">
</div>
<div class="group-options__item">
<input type="radio" name="group-4">
<input type="radio" name="group-4" data-prop="prop4" checked>
<input type="radio" name="group-4" data-prop="prop4">
</div>
</div>
<div class="group-options__item">
<input type="radio" name="group-5">
<input type="radio" name="group-5" data-prop="prop5" checked>
<input type="radio" name="group-5" data-prop="prop5">
</div>
<div class="group-options__item">
<input type="radio" name="group-6" data-prop="prop6" checked>
<input type="radio" name="group-6" data-prop="prop6">
</div>
const wrappers = document.querySelectorAll('.group-options__item');
const selectNoEmptyButton = wrapper => {
const elements = wrapper.querySelectorAll('input[type="radio"]');
const notEmptyElement = [...elements].find(element => !!element.getAttribute('data-prop'));
notEmptyElement.setAttribute('checked', 'checked');
};
[...wrappers].forEach(wrapper => selectNoEmptyButton(wrapper));
Answer the question
In order to leave comments, you need to log in
$('.group-options__item').each(function() {
const $this = $(this);
const $group = $this.closest('.group-options');
($group.length ? $group : $this).find('[data-prop]').first().prop('checked', true);
});
Array
.from(
document.querySelectorAll('.group-options__item'),
n => (n.closest('.group-options') || n).querySelector('[data-prop]')
)
.filter(Boolean)
.forEach(n => n.checked = true);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question