Multiple document.createElement with eventListeners in One Function Return?
NickName:AnotherMike Ask DateTime:2016-02-23T10:55:54

Multiple document.createElement with eventListeners in One Function Return?

I have a function that returns something into the dom (a cellrenderer in ag-grid). Is it possible to create two different elements with document.createElement(), attach event listeners with addEventListener(), and return them in a single function?

For instance, this works, but I need to return two elements:

function() {

  var el = document.createElement('input');

  el.addEventListener('click', function () {
       console.log("el clicked");
  });

  return el;

}

//returns <input/> into the dom, and attaches eventlistener

Here is what I'm trying to do. The only way I have been able to get it to return html is by adding outerHTML methods on the return, but it doesn't seem to add the event listeners that way. If you take off .outerHTML it will return [object HTMLInputElement][object HTMLButtonElement]

function() {

  var el = document.createElement('input');
  var el2 = document.createElement('button');

  el.addEventListener('click', function () {
       console.log("el clicked");
  });
  el2.addEventListener('click', function () {
       console.log("el2 clicked");
   });

  return el.outerHTML + el2.outerHTML

}

//want this to return <input/><button/> with eventlisteners attached

Copyright Notice:Content Author:「AnotherMike」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/35567937/multiple-document-createelement-with-eventlisteners-in-one-function-return

More about “Multiple document.createElement with eventListeners in One Function Return?” related questions

Multiple document.createElement with eventListeners in One Function Return?

I have a function that returns something into the dom (a cellrenderer in ag-grid). Is it possible to create two different elements with document.createElement(), attach event listeners with

Show Detail

Eventlisteners impact?

What impact does eventlisteres have? Im talking about big numbers, here's an example: There's only x amount of .marker at first All children are added via JS when .marker is clicked - eventlistene...

Show Detail

Using two eventlisteners for one function in Actionscript

I'm making a dots and boxes game in flash using actionscript 3. Currently I'm completely stuck as I want the player to click two buttons before a line is displayed. I planned to do something like ...

Show Detail

JS - multiple eventlisteners and dynamic arguments in one line of code

I'm currently trying to get my head around adding multiple eventlisteners with dynamic arguments in one line of code. More specific I want the input[name='txtName'] to call a function when blur or...

Show Detail

Javascript eventlisteners override other buttons?

I've run into a problem using eventlisteners and javascript (though onclick seems to have the same effect). I have this in html &lt;input id="srch" type="button" value="Zoek gebruikers!" oncli...

Show Detail

Simultaneous eventListeners on multiple elements

I built a simple input group using bootstrap and am trying to make the "Run / Go" button only clickable once all input was passed in: &lt;div class="form-group"&gt; &lt;!-- Input one --&gt; &l..

Show Detail

add multiple EventListeners in if statement

Is it possible to check for multiple eventlisteners before executing af function?. the following code is intended that, when i click a button, it then checks if the two elements are clicked on, a...

Show Detail

Sorting an array of objects by property using eventlisteners with a dropdown list

So I'm creating a view that shows a list of ads. Each ad is an object in an array and has properties (title, description, dates, etc.) I wrote the javascript to display them in order and created a...

Show Detail

Can we return document.createElement("element")?

I have to create an input box multiple times, so I call a function like this multiple times: function __createInputBox(id) { var input = document.createElement("input"); input.setAttribut...

Show Detail

eventListeners Won't Leave

These are the last two lines of action script for my frame : removeListeners(); if(!stage.hasEventListener(Event.ENTER_FRAME)){trace("STAGE HAS NO eventListeners");} With the removeListeners() fu...

Show Detail