Knockout.js and custom attribute
Say you doing MVVM binding with Knockout. Say you want to create an attribute on a HTML element that isn’t proper HTML.
1 | <a href="#" data-bind="attr: { 'userId' : id }" class="deleteLink">delete</a> |
The trick is to set the attribute name as a string: ‘userId’. This is proper JSON. To be honest – it is the proper way to create JSON but since javascript solves it anyway we usually omit it.
Now for the second part. How do we get it again?
1 | /element/.attributes["userId"].value |
is the trick.
Like so:
1 2 3 4 | $.(".deleteLink").live( "click", delete_Click ); function delete_Click(){ alert( this.attributes["userId"].value ); } |
Tags: knockout.js