admin管理员组文章数量:1435859
I'm looking to create a custom event in HTML, JS.
<input type="text" oncustombind="foo(event);" />
How can I create one such? 'oncustombind' is the custom event I want to create. I should be able to call the function/code defined in the oncustombind attribute. Also, I need to pass some data to the event object.
I don't want to use any libraries such as jquery, YUI.
Any help would be deeply appreciated.
I'm looking to create a custom event in HTML, JS.
<input type="text" oncustombind="foo(event);" />
How can I create one such? 'oncustombind' is the custom event I want to create. I should be able to call the function/code defined in the oncustombind attribute. Also, I need to pass some data to the event object.
I don't want to use any libraries such as jquery, YUI.
Any help would be deeply appreciated.
Share Improve this question asked Oct 28, 2011 at 9:26 varunvsvarunvs 8951 gold badge12 silver badges24 bronze badges 2 |2 Answers
Reset to default 11You want a CustomEvent
They should be easy to work with but browser support is poor. However the DOM-shim should fix that.
var ev = new CustomEvent("someString");
var el = document.getElementById("someElement");
el.addEventListener("someString", function (ev) {
// should work
});
el.dispatchEvent(ev);
It's bad practice to use new Function
and with
, but this can be accomplished as follows: http://jsfiddle.net/pimvdb/72GwE/13/.
function trigger(elem, name, e) {
var func = new Function('e',
'with(document) {'
+ 'with(this) {'
+ elem.getAttribute('on' + name)
+ '}'
+ '}');
func.call(elem, e);
}
With the following HTML:
<input type="text" oncustombind="console.log(e, type, getElementById);" id="x">
then trigger the event like:
trigger(document.getElementById('x'), 'custombind', {foo: 123});
本文标签: Custom event in HTMLJavaScriptStack Overflow
版权声明:本文标题:Custom event in HTML, Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739341747a2158966.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
this
context of the web component with the chidlren dom element usingObject.assign
. In this way any selected Dom element can have access to methods defined in the web components class. It would be even nicer to have access to custom events from inline custom event handlers. – Adrian Moisa Commented Dec 10, 2017 at 8:46