creating buttons in a loop and adding events with addEventHandler

in Script development
Subscribe to creating buttons in a loop and adding events with addEventHandler 11 posts, 3 voices



v2 Scriptwright
FirefoxX11

hi

i'm creating buttons in a loop and try to add EventListeners like this:

// cr buttons
for (var key in button) {
	var barr = new Array();
	barr[key] = document.createElement("button");
	barr[key].appendChild(document.createTextNode(key));
	barr[key].name = key;
	barr[key].value = button[key];
	barr[key].type = "button";
	barr[key].addEventListener('click', funcGen(button[key]), true);
	emodiv.appendChild(barr[key]);
}

function funcGen(text){
  alert("calling funcGen for " + text);
  return function () {
    alert("meh: " + text);  // or whatever you need to do
  }
}
alert("calling funcGen for " + text); gets called..
but nothing happens when i click a button

i'm using GM 0.8.20090123.1 with FF 3.5.3 (on linux - x86_64).

any advice?

TIA, v2

 
GIJoe Scriptwright
SeamonkeyMacintosh

Try this:

// cr buttons
for (var key in button) {
	var barr = document.createElement("button");
	barr.textContent = key;
	barr.name = key;
	barr.value = button[key];
	barr.type = "button";
	barr.addEventListener('click', funcGen, true);
	emodiv.appendChild(barr);
}

function funcGen(event) {
  var text = event.target.value;
  alert("calling funcGen for " + text);
}

 
v2 Scriptwright
FirefoxX11

@GIJoe
Creates the buttons as expected but now nothing happens. no alert at all :/

 
GIJoe Scriptwright
SeamonkeyMacintosh

It's working...
Make sure you copied correctly the code.

 
IRoll11!~s Scriptwright
FirefoxWindows

Or this:

for (var key in button) {
	var barr = document.createElement("button");
        var buttonvalue = button[key];
	barr.appendChild(document.createTextNode(key));
	barr.name = key;
	barr.value = buttonvalue;
	barr.type = "button";
	barr.addEventListener('click', function(buttonvalue){
            return function(ev){
               alert(ev.target);
               alert(buttonvalue);
	    };
        }(buttonvalue), false);
	emodiv.appendChild(barr);
}

 
v2 Scriptwright
FirefoxX11

I'm pretty sure i did...
But it's not working here.. maybe a Firefox bug?

 
GIJoe Scriptwright
SeamonkeyMacintosh

Perhaps a problem in an other part of your script ?

 
IRoll11!~s Scriptwright
FirefoxWindows

Actually I would modify it more to this:

for (var key in button) {
        var buttonvalue = button[key];
	var singlebutton = document.createElement("button");
	singlebutton.appendChild(document.createTextNode(key));
	singlebutton.setAttribute('name',key);
	singlebutton.setAttribute('value',buttonvalue);
	singlebutton.setAttribute('type',"button");
	emodiv.appendChild(singlebutton);
	singlebutton.addEventListener('click', function(buttonvalue){
            return function(ev){
               alert(ev.target);
               alert(buttonvalue);
	    };
        }(buttonvalue), false);
}

 
v2 Scriptwright
FirefoxX11

i sent you both a pm with the full script.
can you test it and tell me if it works for you? :/

 
GIJoe Scriptwright
SeamonkeyMacintosh

don't use

div.innerHTML = div.innerHTML + '';
It kill all the events.

 
v2 Scriptwright
FirefoxX11

awesome. it works now... thanks! :)

Cross
Presentational HTML allowed.
Use <code> for inline code and <pre> for code blocks. Use &lt; and &gt; for literal < and >.
We help break paragraphs and link your links.
or cancel