# Attribute Binding

The [Kendo UI Attribute (`attr`) binding](https://demos.telerik.com/kendo-ui/mvvm/attributes) populates DOM element attributes from View-Model fields or methods.

This is useful in many cases, such as setting the `href` and `title` of a hyperlink, setting the `src` attribute of an image, etc. If the View-Model fields change, the attributes get updated.

> The [Kendo UI widgets](https://demos.telerik.com/kendo-ui/) do not support the `attr` binding.

## Getting Started

To set the `attr` binding, use the `attr: { attribute1: field1, attribute2: field2 }` configuration. `attribute1` and `attribute2` are the names of the attributes that will be set, and `field1` and `field2` are the names of the View-Model fields to which those attributes will be bound.

The following example demonstrates how to implement the Attribute binding. The `src` and `alt` attributes of the image are bound to the View-Model.

```
<img id="logo" data-bind="attr: { src: imageSource, alt: imageAlt }" />
<script>
var viewModel = kendo.observable({
    imageSource: "https://www.telerik.com/image/kendo-logo.png",
    imageAlt: "Kendo Logo"
});

kendo.bind($("#logo"), viewModel);
</script>
```

After calling `kendo.bind`, the image looks in the following way. The `data-bind` attribute is removed for clarity. Changing the `imageSource` or `imageAlt` fields will update the `src` and `alt` attributes respectively.

```
<img id="logo" src="https://www.telerik.com/image/kendo-logo.png" alt="Kendo Logo"" />
```

## Important Notes

* Binding Attributes: value and checked—To set the `value` or `checked` attributes, use the [`value`](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/bindings/value) and [`checked`](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/bindings/checked) bindings instead.

* Setting the HTML5 `Data` attributes—You can also set HTML5 data attributes through the `attr` binding.

  ```
    <div data-bind="attr: { data-foo: fooValue, data-bar: barValue }"></div>

    <script>
    var viewModel = kendo.observable({
        fooValue: "foo",
        barValue: "bar"
    });

    kendo.bind($("div"), viewModel);
    </script>
  ```

* Attribute sanitization—Attributes with invalid names and `srcdoc` are not set. The URL attributes (such as `href`, `src`, `action` and `formaction`) with unsafe protocols are replaced with `#INVALIDLINK`. Event handler attributes (names starting with `on`) are preserved for backward compatibility. Their values must come from trusted sources because the browser can execute them as JavaScript. Prefer the [`events`](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/bindings/events) binding for event handlers.

## See Also

* [MVVM Overview](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/overview)
* [Overview of the Checked Binding](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/bindings/checked)
* [Overview of the Click Binding](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/bindings/click)
* [Overview of the CSS Binding](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/bindings/css)
* [Overview of the Custom Binding](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/bindings/custom)
* [Overview of the Disabled Binding](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/bindings/disabled)
* [Overview of the Enabled Binding](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/bindings/enabled)
* [Overview of the Events Binding](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/bindings/events)
* [Overview of the HTML Binding](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/bindings/html)
* [Overview of the Invisible Binding](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/bindings/invisible)
* [Overview of the Source Binding](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/bindings/source)
* [Overview of the Style Binding](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/bindings/style)
* [Overview of the Text Binding](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/bindings/text)
* [Overview of the Value Binding](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/bindings/value)
* [Overview of the Visible Binding](https://www.telerik.com/kendo-jquery-ui/documentation/framework/mvvm/bindings/visible)
