{"record":{"id":"164202391ee4c6cb","repo":"cheeriojs/cheerio","slug":"bad-combination-of-arguments","errorCode":null,"errorMessage":"Bad combination of arguments.","messagePattern":"Bad combination of arguments\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/api/attributes.ts","lineNumber":204,"sourceCode":" * @see {@link https://api.jquery.com/attr/}\n */\nexport function attr<T extends AnyNode>(\n  this: Cheerio<T>,\n  values: Record<string, string | null>,\n): Cheerio<T>;\nexport function attr<T extends AnyNode>(\n  this: Cheerio<T>,\n  name?: string | Record<string, string | null>,\n  value?:\n    | string\n    | null\n    | ((this: Element, i: number, attrib: string) => string | null),\n): string | Cheerio<T> | undefined | Record<string, string> {\n  // Set the value (with attr map support)\n  if (typeof name === 'object' || value !== undefined) {\n    if (typeof value === 'function') {\n      if (typeof name !== 'string') {\n        throw new TypeError('Bad combination of arguments.');\n      }\n      return domEach(this, (el, i) => {\n        if (isTag(el)) setAttr(el, name, value.call(el, i, el.attribs[name]));\n      });\n    }\n    return domEach(this, (el) => {\n      if (!isTag(el)) return;\n\n      if (typeof name === 'object') {\n        for (const [objName, objValue] of Object.entries(name)) {\n          setAttr(el, objName, objValue);\n        }\n      } else {\n        if (typeof name !== 'string') {\n          throw new TypeError('Bad combination of arguments.');\n        }\n        setAttr(el, name, value ?? null);\n      }","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/cheeriojs/cheerio/blob/a1be131f9b0cef323ef0d65c5babda561413ac7d/src/api/attributes.ts#L186-L222","documentation":"Cheerio's attr() method only accepts certain argument combinations. When the second argument (value) is a function, the first argument (name) must be a string attribute name. Calling attr() with a function value but a non-string name (e.g. an object map combined with a function) is invalid and throws immediately.","triggerScenarios":"Calling $('.x').attr({...}, function(){...}) — i.e. passing an object of attributes as the first argument AND a function as the second argument. The function-value overload requires attr(name: string, fn).","commonSituations":"Copy-pasting between jQuery-style code where option objects and callbacks were mixed; refactoring attr calls and accidentally leaving both an options object and a callback; TypeScript users bypassing types with any.","solutions":["Change the call to pass a string attribute name with the function: attr('href', (i, old) => ...)","If setting multiple attributes, drop the function and pass only the object: attr({href: '...', id: '...'})","Enable TypeScript types so the invalid overload is caught at compile time"],"exampleFix":"// before\n$('img').attr({ src: 'a.png' }, (i, old) => old);\n// after\n$('img').attr('src', (i, old) => old);","handlingStrategy":"type-guard","validationCode":"const isAttrFnCall = (name: unknown, value: unknown) =>\n  typeof value === 'function' ? typeof name === 'string' : typeof name === 'string' || typeof name === 'object';\nif (!isAttrFnCall(name, value)) throw new Error('Invalid attr() arguments');","typeGuard":"function isValidAttrArgs(\n  name: unknown,\n  value?: unknown,\n): name is string | Record<string, string> {\n  if (typeof value === 'function') return typeof name === 'string';\n  return typeof name === 'string' || (typeof name === 'object' && name !== null);\n}","tryCatchPattern":"try { $('a').attr(name, value); } catch (e) { if (e instanceof TypeError && e.message === 'Bad combination of arguments.') { /* log and fix args */ } else throw e; }","preventionTips":["Use TypeScript overloads — they reject invalid attr() combos at compile time","Never combine an object map with a function value","Wrap dynamic selector/attr code in small typed helpers"],"tags":["cheerio","attr","arguments","typeerror","dom-manipulation"],"backgroundTag":"invalid-argument-combination","analyzedSha":"a1be131f9b0cef323ef0d65c5babda561413ac7d","analyzedAt":"2026-08-28T13:05:26.741Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}