I have HTML code like so:
<label class="switch" value="true">
<input name="source" type="checkbox"/>
</label>
How can I access input with name source
? I have tried using getByRole
and name
attribute, but it throws an error that this input has name=""
.
Below is the tried solution that doesn't work:
screen.getByRole('checkbox', { name: 'source' });
How can I access this input element in other way?
I have tried to do like so as per solution provided
const elem = document.querySelector(
`input[name="source"]`);
if (elem) {
userEvent.click(elem);
}
This works but if I remove if statement like so
const elem = document.querySelector(
`input[name="source"]`);
userEvent.click(elem);
I get an error:
Argument of type null| element is not assignable to parameter of type 'targetelement'. type null is not assignable to type targetelement