Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When submitting a form, the event.target is not the proper node. #1544

Open
juandiegombr opened this issue Sep 18, 2024 · 1 comment
Open
Labels
bug Something isn't working

Comments

@juandiegombr
Copy link
Contributor

Describe the bug
When submitting a form, the form element obtained via the event.target is different from the form element obtained via document.getElementById(formId) or document.forms.namedItem(formId)

To Reproduce
This test should pass:

it('submit target node is equal to node retrieved from document', () => {
	const element = <HTMLFormElement>document.createElement('form');
	element.setAttribute('id', 'form-id');
	element.innerHTML = `
						<div>
								<input type="text" name="text1">
						</div>
				`;

	document.body.appendChild(element);

	let submitEvent: Event | null = null;
	let target: EventTarget | null = null;

	element.addEventListener('submit', (event: Event) => {
		submitEvent = event;
		target = event.target;
	});

	element.action = 'https://localhost:3000';
	element.noValidate = true;
	element.requestSubmit();

	const form = document.forms.namedItem('form-id');
	expect(form === target).toBeTruthy();
});

Expected behavior
Both nodes should be the same.

Additional context
This problem came up when using conform-to/react. In the source code of this library makes this comparison. While in the browser works well it fails if a try to test it.

https://github.com/edmundhung/conform/blob/main/packages/conform-dom/form.ts#L784-L794

@juandiegombr juandiegombr added the bug Something isn't working label Sep 18, 2024
@juandiegombr
Copy link
Contributor Author

I am trying to figure out how to solve this issue but I don't find in the source code how the node is injected in the event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant