feat(attributes): support button value on .val() (#4175)

This commit is contained in:
Kaio Duarte
2026-01-21 11:35:44 +00:00
committed by GitHub
parent c78286486d
commit 08326d0e8c
3 changed files with 12 additions and 1 deletions

View File

@@ -93,6 +93,8 @@ export const unwrapspans: string = [
].join('');
export const inputs: string = [
'<button id="btn-value" value="button">Button</button>',
'<button id="btn-valueless">Button</button>',
'<select id="one"><option value="option_not_selected">Option not selected</option><option value="option_selected" selected>Option selected</option></select>',
'<select id="one-valueless"><option>Option not selected</option><option selected>Option selected</option></select>',
'<select id="one-html-entity"><option>Option not selected</option><option selected>Option &lt;selected&gt;</option></select>',

View File

@@ -652,6 +652,14 @@ describe('$(...)', () => {
expect($('<div>').val()).toBeUndefined();
});
it('(): on button should get value', () => {
const val = $('#btn-value').val();
expect(val).toBe('button');
});
it('(): on button with no value should get undefined', () => {
const val = $('#btn-valueless').val();
expect(val).toBeUndefined();
});
it('(): on select should get value', () => {
const val = $('select#one').val();
expect(val).toBe('option_selected');
@@ -1025,7 +1033,7 @@ describe('$(...)', () => {
it('(fn) : should no op elements without attributes', () => {
const $inputs = $(inputs);
const val = $inputs.removeClass(() => 'tasty');
expect(val).toHaveLength(15);
expect(val).toHaveLength(17);
});
it('(fn) : should skip text nodes', () => {

View File

@@ -811,6 +811,7 @@ export function val<T extends AnyNode>(
? option.toArray().map((el) => text(el.children))
: option.attr('value');
}
case 'button':
case 'input':
case 'option': {
return querying