{"record":{"id":"2c9b977bef89ee87","repo":"grafana/k6","slug":"the-argument-to-filter-must-be-a-function-a-sel","errorCode":null,"errorMessage":"the argument to filter() must be a function, a selector or a selection","messagePattern":"the argument to filter\\(\\) must be a function, a selector or a selection","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"js/modules/k6/html/html.go","lineNumber":393,"sourceCode":"\t\t\tcommon.Throw(s.rt, fmt.Errorf(\"the function passed to each() failed: %w\", err))\n\t\t}\n\t}\n\n\treturn Selection{s.rt, s.sel.Each(fn), s.URL}\n}\n\nfunc (s Selection) Filter(v sobek.Value) Selection {\n\tswitch val := v.Export().(type) {\n\tcase string:\n\t\treturn Selection{s.rt, s.sel.Filter(val), s.URL}\n\n\tcase Selection:\n\t\treturn Selection{s.rt, s.sel.FilterSelection(val.sel), s.URL}\n\t}\n\n\tsobekFn, isFn := sobek.AssertFunction(v)\n\tif !isFn {\n\t\tcommon.Throw(s.rt, errors.New(\"the argument to filter() must be a function, a selector or a selection\"))\n\t}\n\n\treturn Selection{s.rt, s.sel.FilterFunction(s.buildMatcher(v, sobekFn)), s.URL}\n}\n\nfunc (s Selection) Is(v sobek.Value) bool {\n\tswitch val := v.Export().(type) {\n\tcase string:\n\t\treturn s.sel.Is(val)\n\n\tcase Selection:\n\t\treturn s.sel.IsSelection(val.sel)\n\n\tdefault:\n\t\tsobekFn, isFn := sobek.AssertFunction(v)\n\t\tif !isFn {\n\t\t\tcommon.Throw(s.rt, errors.New(\"the argument to is() must be a function, a selector or a selection\"))\n\t\t}","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/js/modules/k6/html/html.go#L375-L411","documentation":"Selection.Filter() in k6's HTML module accepts exactly three argument kinds: a CSS selector string, another Selection, or a predicate function. The Go code first tries the string and Selection cases via a type switch, then falls back to sobek.AssertFunction; anything that is none of the three throws this error.","triggerScenarios":"Calling filter() with a DOM Element (e.g. an item obtained from each()), a number, null/undefined, or an array of nodes.","commonSituations":"Passing an element handle where a selector or function was intended; calling filter() with no arguments; mixing up the k6/html API with browser-module Element APIs.","solutions":["Use a selector string: sel.filter('.active')","Or a function: sel.filter((idx, el) => el.attr('data-ok') === '1')","Or wrap an existing selection: sel.filter(otherSelection)"],"exampleFix":"// before\nselection.filter(someElement);\n\n// after\nselection.filter((idx, el) => el.id() === someElement.id());","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"const isFilterArg = (v) =>\n  typeof v === 'string' ||\n  (v !== null && typeof v === 'object' && 'sel' in v && typeof v === 'object' && typeof v.sel !== 'undefined') ||\n  typeof v === 'function';\nif (!isFilterArg(arg)) throw new TypeError('filter() expects selector, Selection, or function');\nsel.filter(arg);","tryCatchPattern":null,"preventionTips":["Pass selectors as strings, comparisons as functions, never raw elements","Confirm variables are defined before using them as filter arguments","Review ported jQuery code for element-argument patterns"],"tags":["javascript","html","dom","filtering","k6-html"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}