0
\$\begingroup\$

I am wondering if the following is a good concideration. There might be several links on a page with a data-rel attribute that could be static or dynamically added with javascript. Instead of adding an eventlistener to each of those I am thinking about adding an eventlistener to the document. I don't want to use jQuery.

Is the following good, or is there a better do add events to dynamically added ellements?

var api = {
 function addEvent (evnt, el, func) {
 if (el.addEventListener) { // W3C DOM
 el.addEventListener(evnt,func,false);
 } else if (el.attachEvent) { // IE DOM
 el.attachEvent('on' + evnt, func);
 } else { // No much to do
 el[evnt] = func;
 }
 }
 , removeEvent : function removeEvent (evnt, el, func) {
 if (el.removeEventListener) { // W3C DOM
 el.removeEventListener(evnt,func,false);
 } else if (el.detachEvent) { // IE DOM
 el.detachEvent('on' + evnt, func);
 } else { // No much to do
 el.splice(evnt, 1);
 }
 }
 , checkWhatIsClicked: function checkIfDialog (e) {
 var el = e.srcElement || e.target
 , dataCallback = el.getAttribute('data-callback')
 , dataRel = el.getAttribute('data-rel');
 if (dataRel !== null) {
 switch (dataRel) {
 case 'alert':
 e.preventDefault();
 dialogBox.alert();
 break;
 case 'dialogBox':
 e.preventDefault();
 dialogBox.dialog();
 break;
 case 'prompt':
 e.preventDefault();
 dialogBox.prompt();
 break;
 }
 } else if (dataCallback !== null && el.className.indexOf('dialog-answer') > -1) {
 if (typeof window[dataCallback] === 'function') {
 var result = el.getAttribute('data-result');
 window[dataCallback](result, el);
 }
 }
 }
};
api.addEvent('click', window.document, api.checkWhatIsClicked);
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Apr 3, 2015 at 18:53
\$\endgroup\$
4
  • 1
    \$\begingroup\$ So what is your question? \$\endgroup\$ Commented Apr 3, 2015 at 18:59
  • \$\begingroup\$ if it is a good approach, or is there a better way to do this? \$\endgroup\$ Commented Apr 3, 2015 at 19:02
  • \$\begingroup\$ addEvent(), click(), removeEvent()? Now that doesn't make sense. Why not just invoke the listener directly? \$\endgroup\$ Commented Apr 3, 2015 at 19:02
  • \$\begingroup\$ Because I don't know how to attach it automaticaly when some element is dynamically added. \$\endgroup\$ Commented Apr 3, 2015 at 19:04

1 Answer 1

1
\$\begingroup\$

What's wrong with jQuery? It doesn't get much easier than:

$(document).delegate("<identifier>", "<event>", function (e) { do stuff; });

However, the following link might help you with base javascript event listening and delegation. https://mattandre.ws/2014/08/small-beautiful-dom-delegation/

answered Apr 3, 2015 at 19:08
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.