# To create custom attribute on input html element

**URL:** <https://discourse.aurelia.io/t/to-create-custom-attribute-on-input-html-element/2658>\
**Category:** Help Requests\
**Created:** [June 18, 2019, 4:08pm UTC](https://discourse.aurelia.io/t/to-create-custom-attribute-on-input-html-element/2658 "2019-06-18T16:08:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chethandb](https://avatars.discourse-cdn.com/v4/letter/c/e68b1a/32.png) [@chethandb](https://discourse.aurelia.io/u/chethandb)\
**Post date:** [June 18, 2019, 4:08pm UTC](https://discourse.aurelia.io/t/to-create-custom-attribute-on-input-html-element/2658/1 "2019-06-18T16:08:08Z")

</div>

I want to create a custom attribute _input-format_  
I can assign two values to the attribute that would enable me to clean the input string or to trim input string. Like below:

For value _clean_:

Trying to achieve declaring as below

```
    <input
         type="text"
         placeholder="enter text with illegal characters"
         value.bind="dirtyString"
         input-format= "format : clean"
    />

```

The above should result me with the below embedded into the input element:

```
     <input
            type="text"
            placeholder="enter text with illegal characters"
            value.bind="dirtyString | cleanString & updateTrigger:'blur'"
            keypress.delegate="keypress($event)"
      />

```

Similarly for _trim_

```
    <input
         type="text"
         placeholder="enter your name"
         value.bind="Name"
         input-format="format : trim"
     />

```

should result in:

```
    <input
        type="text"
        placeholder="enter your name"
        value.bind="Name | trimString & updateTrigger:'blur'"
    />

```

where cleanString, trimString, are value converters and keypress function which are already declared as needed. I need help with creating custom attribute as I am not sure of getting the current _value_ bound to an html input element and also reassigning that to have all the above value converters and a function.

Can anyone please help me on achieving this? Appreciate your input and help.

This is where I stand:

```
import * as au from "aurelia-framework";

@au.autoinject
@au.customAttribute("input-format")
export class InputFormat {
  constructor(element: Element) {
    this.element = element;
  }

  @au.bindable
  format: string;
  formatChanged(name: string, newValue: string, oldValue: string) {
    // need to have case statements 
    switch(name){
        case 'clean':
          // to assign the relevant value converter and the 'value' is to be passed into
          // this assigment should result something like below
          // <input
          // type="text"
          // value.bind="Name | cleanString & updateTrigger:'blur'"
          // keypress.delegate="keypress($event)"
         // >
          break;
        case 'trim':
          // to assign the relevant value converter the 'value' is to be passed into
          // <input
          // type="text"
          // value.bind="Name | trimString"          
         // >
          break;
        default:
          // to leave the assigment as is 
          break;
      }
    
  }
  // the select list
  element: Element;
  val: any;
}

```

here is the link where I am trying to put things together:  
[sand box link](https://codesandbox.io/embed/custom-attribute-for-clean-string-xm531)

---

<div class="post-metadata">

**Author:** ![MaximBalaganskiy](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/maximbalaganskiy/32/831_2.png) [@MaximBalaganskiy](https://discourse.aurelia.io/u/MaximBalaganskiy)\
**Post date:** [June 19, 2019, 1:24am UTC](https://discourse.aurelia.io/t/to-create-custom-attribute-on-input-html-element/2658/2 "2019-06-19T01:24:33Z")

</div>

How about a custom behavior instead? Then you would  
`<input value.bind='dirtyString & format: 'trim'>`

---

<div class="post-metadata">

**Author:** ![chethandb](https://avatars.discourse-cdn.com/v4/letter/c/e68b1a/32.png) [@chethandb](https://discourse.aurelia.io/u/chethandb)\
**Post date:** [June 19, 2019, 6:46pm UTC](https://discourse.aurelia.io/t/to-create-custom-attribute-on-input-html-element/2658/3 "2019-06-19T18:46:25Z")

</div>

Could you please give me an example of how you would code that part. Like how would you let input element’s _value_ know to consider the evaluating ‘format’ option. Any working sandbox would be of great help so I can try out things.

---

<div class="post-metadata">

**Author:** ![MaximBalaganskiy](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/maximbalaganskiy/32/831_2.png) [@MaximBalaganskiy](https://discourse.aurelia.io/u/MaximBalaganskiy)\
**Post date:** [June 20, 2019, 7:17am UTC](https://discourse.aurelia.io/t/to-create-custom-attribute-on-input-html-element/2658/4 "2019-06-20T07:17:04Z")

</div>

have a look at the original updateTrigger behaviour [https://github.com/aurelia/templating-resources/blob/master/src/update-trigger-binding-behavior.ts](https://github.com/aurelia/templating-resources/blob/master/src/update-trigger-binding-behavior.ts)  
There are also docs on custom binding behaviours
