{"record":{"id":"249703953fadc2e2","repo":"grafana/k6","slug":"the-argument-to-each-must-be-a-function","errorCode":null,"errorMessage":"the argument to each() must be a function","messagePattern":"the argument to each\\(\\) must be a function","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"js/modules/k6/html/html.go","lineNumber":370,"sourceCode":"\t\treturn s.rt.ToValue(valueOrHTML(selected))\n\n\tdefault:\n\t\treturn sobek.Undefined()\n\t}\n}\n\nfunc (s Selection) Children(def ...string) Selection {\n\tif len(def) == 0 {\n\t\treturn Selection{s.rt, s.sel.Children(), s.URL}\n\t}\n\n\treturn Selection{s.rt, s.sel.ChildrenFiltered(def[0]), s.URL}\n}\n\nfunc (s Selection) Each(v sobek.Value) Selection {\n\tsobekFn, isFn := sobek.AssertFunction(v)\n\tif !isFn {\n\t\tcommon.Throw(s.rt, errors.New(\"the argument to each() must be a function\"))\n\t}\n\n\tfn := func(idx int, _ *goquery.Selection) {\n\t\tif _, err := sobekFn(v, s.rt.ToValue(idx), selToElement(Selection{s.rt, s.sel.Eq(idx), s.URL})); err != nil {\n\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}","sourceCodeStart":352,"sourceCodeEnd":388,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/js/modules/k6/html/html.go#L352-L388","documentation":"In k6's HTML module (parseHTML), Selection.Each() mirrors jQuery's each() and only accepts a callback function invoked as (index, element). The Go implementation calls sobek.AssertFunction and throws immediately when the argument is not callable. Note that unlike jQuery, a selector string is not accepted here.","triggerScenarios":"Calling sel.each() with anything other than a function: a selector string ('sel.each(\"div\")'), no argument (undefined), or an object.","commonSituations":"Porting jQuery code where a string filter was tolerated; forgetting the callback during refactoring; passing a Selection instead of iterating its elements.","solutions":["Pass a function: sel.each((i, el) => { ... })","Filter first with .find()/.filter() if the intent was to narrow the selection, then call each() with the callback","If iterating without an index, still declare the callback signature (idx is always provided)"],"exampleFix":"// before\nselection.each('div');\n\n// after\nselection.find('div').each((idx, el) => { console.log(el.nodeName()); });","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"const isFn = (v) => typeof v === 'function';\nif (!isFn(cb)) throw new TypeError('each() expects a function');\nsel.each(cb);","tryCatchPattern":null,"preventionTips":["Check typeof cb === 'function' before each() when the callback is dynamic","Remember k6/html each() accepts no selector string (unlike some jQuery usages)","Filter with find()/filter() first, then iterate with a callback"],"tags":["javascript","html","dom","iteration","k6-html"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}