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

HTMLOptionElement.selected not behaving correctly #1594

Open
coffeeandwork opened this issue Nov 8, 2024 · 0 comments
Open

HTMLOptionElement.selected not behaving correctly #1594

coffeeandwork opened this issue Nov 8, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@coffeeandwork
Copy link

Describe the bug
If you have a <select> element with the multiple attribute set, with 2 values selected, and you unselect one of the values programmatically, the HTMLSelectElement.selectedOptions does not seem to show have the correct values.

To Reproduce
Steps to reproduce the behavior:

  1. Create a new file test.js with the following code:
import { GlobalRegistrator } from '@happy-dom/global-registrator';
GlobalRegistrator.register();
function addNewOptionElement(selectElement, val) {
  const option = document.createElement('option');
  option.value = val;
  option.text = val;
  selectElement.appendChild(option);
  return option;
}
const select = document.createElement('select');
select.multiple = true;
document.body.appendChild(select);
const option1 = addNewOptionElement(select, 'Option 1');
const option2 = addNewOptionElement(select, 'Option 2');
const option3 = addNewOptionElement(select, 'Option 3');
option1.selected = true;
option2.selected = true;
option3.selected = false;
console.log('total number of selected options:', select.selectedOptions.length);
option1.selected = false;
console.log('total number of selected options:', select.selectedOptions.length);
await GlobalRegistrator.unregister();
  1. Run it the code in command line:

Either using node:

$> node ./test.js

Or, using bun.sh:

$> bun run ./test.js
  1. Both of them produce the following incorrect but identical output:
total number of selected options: 2
total number of selected options: 2

The first value being 2 is correct because we have selected 'Option 1' and 'Option 2'. However, the second value of 2 is incorrect because we have unselected 'Option 1' with the statement option1.selected = false;.

Expected behavior
Here is the code I ran in chrome browser's developer console (only difference being that I have removed the calls to the GlobalRegistrator):

function addNewOptionElement(selectElement, val) {
  const option = document.createElement('option');
  option.value = val;
  option.text = val;
  selectElement.appendChild(option);
  return option;
}
const select = document.createElement('select');
select.multiple = true;
document.body.appendChild(select);
const option1 = addNewOptionElement(select, 'Option 1');
const option2 = addNewOptionElement(select, 'Option 2');
const option3 = addNewOptionElement(select, 'Option 3');
option1.selected = true;
option2.selected = true;
option3.selected = false;
console.log('total number of selected options:', select.selectedOptions.length);
option1.selected = false;
console.log('total number of selected options:', select.selectedOptions.length);

The output showed correctly where as follows:

total number of selected options: 2
total number of selected options: 1

This output is correct.

Screenshots
Screenshot below shows the above test working correctly in Google Chrome
Screenshot 2024-11-07 at 11 16 12 PM

Device:

  • OS: MacOS Sequoia 15.1 (24B83)
  • Browser chrome
  • Version 130.0.6723.116 (Official Build) (arm64)

Additional context
I tried this using the following versions of node and bun.sh:
node: v22.0.0
bun.sh: 1.1.31

@coffeeandwork coffeeandwork added the bug Something isn't working label Nov 8, 2024
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