javascript - document.registerElement extending HTMLInputElement prototype while using custom tag name to avoid implicit browser style -
my goal two-fold:
display html element keeps track of 'checked' state , triggers 'change' events without having manually implement code myself.
not fight browser's implicit styling of element
-webkit-appearance: none
, more fussing.
i need support current, stable channel of google chrome. such, not have concern compatibility non-modern browsers.
i've approached problem using document.registerelement. method, register new tag, custom-checkbox
, , inform browser tag inherit input.
from there, need setup fact input's type checkbox. so, set type in createdcallback
method ran when html element created.
this code has issues:
it works when not use custom tag , instead leverage
is
property inform browser input 'is'custom-checkbox
. however, using<input>
tag causes browser take on styling element. not wish have happen.when using custom tag,
createdcallback
method not ran. such, element not setup fact checkbox , not fire 'change' events when clicked.
what missing?
var proto = object.create(htmlinputelement.prototype); proto.createdcallback = function() { console.log('createdcallback has ran for: ', this); this.type = "checkbox"; }; document.registerelement("custom-checkbox", { prototype: proto, extends: 'input' }); $('.custom-checkbox').change(function() { console.log('change event handler has ran for:', this); });
.custom-checkbox { width: 100px; height: 100px; background-color: red; display: inline-block; }
<custom-checkbox class="custom-checkbox"></custom-checkbox> <input is='custom-checkbox' class="custom-checkbox">
i can duplicate issue createdcallback
method not fire when inserting <custom-element>
tag onto page declared using html
. i'm using chrome version 39.0.2171.99
i believe because when <custom-element>
rendered, event listener createcallback
has not binded.
bear in mind custom elements not yet defined standard (see this)
however, using polymer solved issue me, using custom tags in html
fire polymer refers create
event/callback.
i suggest use polymer, has better browser support to.
Comments
Post a Comment