Matt-Esch / virtual-dom

A Virtual DOM and diffing algorithm
MIT License
11.65k stars 780 forks source link

No for possible on a label element? #278

Closed batiste closed 9 years ago

batiste commented 9 years ago
virtualDom.create(virtualDom.h('label', {for:1, title:2}))
<label title="2"></label>

Why does the "for" attribute doesn't work?

andrewrota commented 9 years ago

The property name for the attribute for is htmlFor [1]. Changing for:1 to htmlFor: 1 should work.

batiste commented 9 years ago

Do you have a list of those properties like class -> className, for -> htmlFor ?

I imagine it is something inherent to the DOM model somehow... It would be nice to not have to care about those details in the API

andrewrota commented 9 years ago

We have an attribute:property mapping on a project that uses virtual-dom because we convert templates to vdom objects and we know everything is getting passed in as an attribute.

You can also pass in attributes directly to virtual-dom by using the attributes property. So your example could also look like this and work:

virtualDom.create(virtualDom.h('label', {attributes:{for: 1}, title:2}))
Matt-Esch commented 9 years ago

virtual-dom doesn't try to have an opinion about this. We just give you a way to set properties and attributes on nodes

feross commented 8 years ago

To anyone finding this in the future... I just published a package hyperscript-attribute-to-property to solve this issue.

Use it like this:

var attrToProp = require('hyperscript-attribute-to-property')
var h = attrToProp(require('virtual-dom/h'))

Then, you can do this:

var vnode = h('div', { class: 'my-class' })

Instead of this:

var vnode = h('div', { className: 'my-class' })

Works with for and the others, too :)