oxc-project/oxc · error

The `a` element has `href` and `onClick`.

Error message

The `a` element has `href` and `onClick`.

What it means

Fires from jsx-a11y/anchor-is-valid when an anchor element has both an href and an onClick handler, meaning it is used as a button. Screen readers may announce it as a link while it behaves like a button; the cant_be_anchor helper reports the span and advises using a button element instead.

Source

Thrown at crates/oxc_linter/src/rules/jsx_a11y/anchor_is_valid.rs:41

        format!("Provide the `{}` attribute for the `a` element.", valid_attrs[0].as_ref())
    } else {
        let list =
            valid_attrs.iter().map(|a| format!("`{}`", a.as_ref())).collect::<Vec<_>>().join(", ");
        format!("Provide one of these attributes for the `a` element: {list}")
    };
    OxcDiagnostic::warn("Missing `href` attribute for the `a` element.")
        .with_help(help)
        .with_label(span)
}

fn incorrect_href(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Use of incorrect `href` for the 'a' element.")
        .with_help("Provide a correct `href` for the `a` element.")
        .with_label(span)
}

fn cant_be_anchor(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("The `a` element has `href` and `onClick`.")
        .with_help("Use a `button` element instead of an `a` element.")
        .with_label(span)
}

#[derive(Debug, Default, Clone, Deserialize)]
pub struct AnchorIsValid(Box<AnchorIsValidConfig>);

#[derive(Debug, Default, Clone, JsonSchema, Deserialize)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct AnchorIsValidConfig {
    /// Custom components to treat as anchor elements.
    components: Vec<CompactStr>,
    /// Custom prop names to treat as link destinations.
    special_link: Vec<CompactStr>,
    /// Sub-rule aspects to run.
    aspects: Option<Vec<AnchorIsValidAspect>>,
}

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Use a `<button type="button">` element instead of the anchor
  2. Remove the onClick handler if the anchor is meant to navigate
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/jsx_a11y/anchor_is_valid.rs:41 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/38cd90ce30fd5f8c. Report an issue: GitHub.