A new variable for Knockout.js bindings – the DOM element
When writing knockout bindings it can sometimes be handy to reference the DOM element you are writing the element for. As an example, you may want information on a DOM property or for development reasons simply want to show the id.
I tweaked Knockout to support using an $element special variable last year, but hadn’t decided to submit it yet. Based on the discussion in knockout issue 176 I decided to clean it up and submit it as knockout pull request 474. Mbest also updated it to work with the latest knockout changes.
Usage is simple:
<div data-bind='text: $element.tagName'></div>
As an aside, I’m going to be presenting on Derby and Meteor at the NodeDC Meetup on 5/16. If you are interested in Node or just want to connect with some cool DC coders stop by!
- Beginners Guide to KnockoutJS: Part 1 (sitepoint.com)
Leave a comment