0

I pretty much wanted the extension to attach buttons in the existing website. I only have one script file in my app, which is main.js

in main.js

table = document.querySelectorAll("table.CG_list.searchList")[0];
div = table.getElementsByClassName("sectionExpandColumn");

var button = document.createElement("a");
button.setAttribute("class", "section");
button.setAttribute("id", "discussion");
button.innerHTML = "Discussion";
button.addEventListener('click', load);

for (var i=0; i<div.length; i++){
div[i].appendChild(button.cloneNode(true));}

function load(e){
   alert("Event is Clicked");}

The buttons are successfully added but when I click the button, nothing is happening. I tried adding the script tag with main.js in the head, but no luck there also.

PowerLove
  • 303
  • 8
  • 25
  • 1
    Possible duplicate of [how to copy a DOM node with event listeners?](http://stackoverflow.com/questions/15408394/how-to-copy-a-dom-node-with-event-listeners) – wOxxOm Aug 22 '16 at 18:20
  • so would it be button.cloneNode.addEventListener()? – PowerLove Aug 22 '16 at 18:25
  • 2
    No, that's invalid syntax. You need an intermediate variable. `var clone = button.cloneNode(); clone.addEventListener(.......); div[i].appendChild(clone);`. In jQuery, though, you would be able to use chaining and avoid the intermediate variable. – wOxxOm Aug 22 '16 at 18:28

0 Answers0