Skip to content
0

katanakit-js / useSetAttribute

Function: useSetAttribute() ​

ts
function useSetAttribute(
   target, 
   attr, 
   value
): void;

Defined in: src/infrastructure/dom/dom.service.ts:250

Sets an attribute on the target element with XSS protection. Blocks event-handler attributes (on*) and javascript: URLs.

Parameters ​

ParameterTypeDescription
targetstring | ElementA CSS selector string or an Element instance.
attrstringThe attribute name.
valuestringThe value to set.

Returns ​

void

Throws ​

If the attribute is an event handler or contains a javascript: URL.

Example ​

ts
useSetAttribute("img.hero", "src", "/photo.jpg");
useSetAttribute("a.link", "href", "https://example.com");

Released under the MIT License.